Bharat saraswal
Committed by Gerrit Code Review

ST defect fixes and review comments fixes

Change-Id: Ib8c56a88c19cd9aa23918d0f9e37c89e74cb0d13
Showing 40 changed files with 1775 additions and 722 deletions
......@@ -22,12 +22,12 @@
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.2.5</version>
<version>3.3.9</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.2.5</version>
<version>3.3.9</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
......@@ -38,18 +38,18 @@
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
<version>1.9.0</version>
<version>1.21.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>3.2.5</version>
<version>3.3.9</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.2.1</version>
<version>3.0-alpha-2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
......@@ -60,12 +60,12 @@
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>3.2.5</version>
<version>3.3.9</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>3.2.5</version>
<version>3.3.9</version>
<scope>test</scope>
</dependency>
......
......@@ -340,9 +340,11 @@ public class YangAugment extends YangNode implements YangLeavesHolder, YangCommo
/**
* Prepare the information for java code generation corresponding to YANG
* grouping info.
*
* @param codeGenDir code generation directory
*/
@Override
public void generateJavaCodeEntry() {
public void generateJavaCodeEntry(String codeGenDir) {
// TODO Auto-generated method stub
}
......
......@@ -337,9 +337,11 @@ public class YangCase extends YangNode
/**
* Generate the code corresponding to YANG case info.
*
* @param codeGenDir code generation directory
*/
@Override
public void generateJavaCodeEntry() {
public void generateJavaCodeEntry(String codeGenDir) {
// TODO Auto-generated method stub
}
......
......@@ -140,22 +140,11 @@ public class YangChoice extends YangNode implements YangCommonInfo, Parsable, Co
super(YangNodeType.CHOICE_NODE);
}
/*
* (non-Javadoc)
*
* @see org.onosproject.yangutils.datamodel.YangNode#getName()
*/
@Override
public String getName() {
return name;
}
/*
* (non-Javadoc)
*
* @see
* org.onosproject.yangutils.datamodel.YangNode#setName(java.lang.String)
*/
@Override
public void setName(String name) {
this.name = name;
......@@ -305,48 +294,24 @@ public class YangChoice extends YangNode implements YangCommonInfo, Parsable, Co
// TODO auto-generated method stub, to be implemented by parser
}
/*
* (non-Javadoc)
*
* @see org.onosproject.yangutils.datamodel.YangNode#getPackage()
*/
@Override
public String getPackage() {
// TODO Auto-generated method stub
return null;
}
/*
* (non-Javadoc)
*
* @see
* org.onosproject.yangutils.datamodel.YangNode#setPackage(java.lang.String)
*/
@Override
public void setPackage(String pkg) {
// TODO Auto-generated method stub
}
/*
* (non-Javadoc)
*
* @see
* org.onosproject.yangutils.translator.CodeGenerator#generateJavaCodeEntry(
* )
*/
@Override
public void generateJavaCodeEntry() {
public void generateJavaCodeEntry(String codeGenDir) {
// TODO Auto-generated method stub
}
/*
* (non-Javadoc)
*
* @see
* org.onosproject.yangutils.translator.CodeGenerator#generateJavaCodeExit()
*/
@Override
public void generateJavaCodeExit() {
// TODO Auto-generated method stub
......
......@@ -16,18 +16,19 @@
package org.onosproject.yangutils.datamodel;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.utils.YangConstructType;
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.YangConstructType;
import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
/*-
* Reference RFC 6020.
......@@ -476,20 +477,24 @@ public class YangContainer extends YangNode implements YangLeavesHolder, YangCom
/**
* Generate the java code corresponding to YANG container.
*
* @throws IOException when fails to generate the source files
* @param codeGenDir code generation directory
* @throws IOException when fails to generate the source files.
*/
@Override
public void generateJavaCodeEntry() throws IOException {
public void generateJavaCodeEntry(String codeGenDir) throws IOException {
YangNode parent = getParent();
String contPkg = JavaIdentifierSyntax.getPackageFromParent(parent.getPackage(), parent.getName());
contPkg = JavaIdentifierSyntax.getCamelCase(contPkg).toLowerCase();
setPackage(contPkg);
CachedFileHandle handle = null;
try {
FileSystemUtil.createPackage(UtilConstants.YANG_GEN_DIR + getPackage(), getName());
FileSystemUtil.createPackage(codeGenDir + getPackage(), parent.getName() + UtilConstants.CHILDREN);
handle = FileSystemUtil.createSourceFiles(getPackage(), getName(),
GeneratedFileType.GENERATE_INTERFACE_WITH_BUILDER);
handle.setRelativeFilePath(UtilConstants.YANG_GEN_DIR + getPackage().replace(".", "/"));
handle.setRelativeFilePath(getPackage().replace(".", "/"));
handle.setCodeGenFilePath(codeGenDir);
} catch (IOException e) {
throw new IOException("Failed to create the source files.");
}
......
......@@ -299,9 +299,11 @@ public class YangGrouping extends YangNode
/**
* Generate the code for YANG grouping.
*
* @param codeGenDir code generated directory.
*/
@Override
public void generateJavaCodeEntry() {
public void generateJavaCodeEntry(String codeGenDir) {
// TODO Auto-generated method stub
}
......
......@@ -622,9 +622,11 @@ public class YangList extends YangNode
/**
* Populate the cached handle with information about the list attributes to
* generate java code.
*
* @param codeGenDir code generated directory
*/
@Override
public void generateJavaCodeEntry() {
public void generateJavaCodeEntry(String codeGenDir) {
// TODO Auto-generated method stub
}
......
......@@ -15,21 +15,21 @@
*/
package org.onosproject.yangutils.datamodel;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.translator.CachedFileHandle;
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.YangConstructType;
import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
/*-
* Reference:RFC 6020.
* The "module" statement defines the module's name,
......@@ -211,7 +211,7 @@ public class YangModule extends YangNode
/**
* Get name of the module.
*
* @return module name.
* @return module name
*/
@Override
public String getName() {
......@@ -221,7 +221,7 @@ public class YangModule extends YangNode
/**
* Set module name.
*
* @param moduleName module name.
* @param moduleName module name
*/
@Override
public void setName(String moduleName) {
......@@ -231,7 +231,7 @@ public class YangModule extends YangNode
/**
* Get the contact details of the module owner.
*
* @return the contact details of YANG owner.
* @return the contact details of YANG owner
*/
public String getContact() {
return contact;
......@@ -240,7 +240,7 @@ public class YangModule extends YangNode
/**
* Set the contact details of the module owner.
*
* @param contact the contact details of YANG owner.
* @param contact the contact details of YANG owner
*/
public void setContact(String contact) {
this.contact = contact;
......@@ -249,7 +249,7 @@ public class YangModule extends YangNode
/**
* Get the description of module.
*
* @return the description of YANG module.
* @return the description of YANG module
*/
@Override
public String getDescription() {
......@@ -259,7 +259,7 @@ public class YangModule extends YangNode
/**
* Set the description of module.
*
* @param description set the description of YANG module.
* @param description set the description of YANG module
*/
@Override
public void setDescription(String description) {
......@@ -269,7 +269,7 @@ public class YangModule extends YangNode
/**
* Get the list of imported modules.
*
* @return the list of imported modules.
* @return the list of imported modules
*/
public List<YangImport> getImportList() {
return importList;
......@@ -278,7 +278,7 @@ public class YangModule extends YangNode
/**
* prevent setting the import list from outside.
*
* @param importList the import list to set.
* @param importList the import list to set
*/
private void setImportList(List<YangImport> importList) {
this.importList = importList;
......@@ -287,7 +287,7 @@ public class YangModule extends YangNode
/**
* Add the imported module information to the import list.
*
* @param importedModule module being imported.
* @param importedModule module being imported
*/
public void addImportedInfo(YangImport importedModule) {
......@@ -303,7 +303,7 @@ public class YangModule extends YangNode
/**
* Get the list of included sub modules.
*
* @return the included list of sub modules.
* @return the included list of sub modules
*/
public List<YangInclude> getIncludeList() {
return includeList;
......@@ -312,7 +312,7 @@ public class YangModule extends YangNode
/**
* Set the list of included sub modules.
*
* @param includeList the included list to set.
* @param includeList the included list to set
*/
private void setIncludeList(List<YangInclude> includeList) {
this.includeList = includeList;
......@@ -321,7 +321,7 @@ public class YangModule extends YangNode
/**
* Add the included sub module information to the include list.
*
* @param includeModule submodule being included.
* @param includeModule submodule being included
*/
public void addIncludedInfo(YangInclude includeModule) {
......@@ -336,7 +336,7 @@ public class YangModule extends YangNode
/**
* Get the list of leaves in module.
*
* @return the list of leaves.
* @return the list of leaves
*/
@Override
public List<YangLeaf> getListOfLeaf() {
......@@ -346,7 +346,7 @@ public class YangModule extends YangNode
/**
* Set the list of leaf in module.
*
* @param leafsList the list of leaf to set.
* @param leafsList the list of leaf to set
*/
private void setListOfLeaf(List<YangLeaf> leafsList) {
listOfLeaf = leafsList;
......@@ -355,7 +355,7 @@ public class YangModule extends YangNode
/**
* Add a leaf in module.
*
* @param leaf the leaf to be added.
* @param leaf the leaf to be added
*/
@Override
public void addLeaf(YangLeaf leaf) {
......@@ -369,7 +369,7 @@ public class YangModule extends YangNode
/**
* Get the list of leaf-list from module.
*
* @return the list of leaf-list.
* @return the list of leaf-list
*/
@Override
public List<YangLeafList> getListOfLeafList() {
......@@ -379,7 +379,7 @@ public class YangModule extends YangNode
/**
* Set the list of leaf-list in module.
*
* @param listOfLeafList the list of leaf-list to set.
* @param listOfLeafList the list of leaf-list to set
*/
private void setListOfLeafList(List<YangLeafList> listOfLeafList) {
this.listOfLeafList = listOfLeafList;
......@@ -388,7 +388,7 @@ public class YangModule extends YangNode
/**
* Add a leaf-list in module.
*
* @param leafList the leaf-list to be added.
* @param leafList the leaf-list to be added
*/
@Override
public void addLeafList(YangLeafList leafList) {
......@@ -402,7 +402,7 @@ public class YangModule extends YangNode
/**
* Get the name space of module elements.
*
* @return the nameSpace.
* @return the nameSpace
*/
public YangNameSpace getNameSpace() {
return nameSpace;
......@@ -411,7 +411,7 @@ public class YangModule extends YangNode
/**
* Set the name space of module elements.
*
* @param nameSpace the nameSpace to set.
* @param nameSpace the nameSpace to set
*/
public void setNameSpace(YangNameSpace nameSpace) {
this.nameSpace = nameSpace;
......@@ -420,7 +420,7 @@ public class YangModule extends YangNode
/**
* Get the modules organization.
*
* @return the organization.
* @return the organization
*/
public String getOrganization() {
return organization;
......@@ -429,7 +429,7 @@ public class YangModule extends YangNode
/**
* Set the modules organization.
*
* @param org the organization to set.
* @param org the organization to set
*/
public void setOrganization(String org) {
organization = org;
......@@ -447,7 +447,7 @@ public class YangModule extends YangNode
/**
* Set the prefix.
*
* @param prefix the prefix to set.
* @param prefix the prefix to set
*/
public void setPrefix(String prefix) {
this.prefix = prefix;
......@@ -456,7 +456,7 @@ public class YangModule extends YangNode
/**
* Get the textual reference.
*
* @return the reference.
* @return the reference
*/
@Override
public String getReference() {
......@@ -466,7 +466,7 @@ public class YangModule extends YangNode
/**
* Set the textual reference.
*
* @param reference the reference to set.
* @param reference the reference to set
*/
@Override
public void setReference(String reference) {
......@@ -476,7 +476,7 @@ public class YangModule extends YangNode
/**
* Get the revision.
*
* @return the revision.
* @return the revision
*/
public YangRevision getRevision() {
return revision;
......@@ -485,7 +485,7 @@ public class YangModule extends YangNode
/**
* Set the revision.
*
* @param revision the revision to set.
* @param revision the revision to set
*/
public void setRevision(YangRevision revision) {
this.revision = revision;
......@@ -494,7 +494,7 @@ public class YangModule extends YangNode
/**
* Get the version.
*
* @return the version.
* @return the version
*/
public byte getVersion() {
return version;
......@@ -503,7 +503,7 @@ public class YangModule extends YangNode
/**
* Set the version.
*
* @param version the version to set.
* @param version the version to set
*/
public void setVersion(byte version) {
this.version = version;
......@@ -558,7 +558,7 @@ public class YangModule extends YangNode
/**
* Get the list of nested reference's which required resolution.
*
* @return list of nested reference's which required resolution.
* @return list of nested reference's which required resolution
*/
public List<YangNode> getNestedReferenceResoulutionList() {
return nestedReferenceResoulutionList;
......@@ -568,7 +568,7 @@ public class YangModule extends YangNode
* Set list of nested reference's which requires resolution.
*
* @param nestedReferenceResoulutionList list of nested reference's which
* requires resolution.
* requires resolution
*/
private void setNestedReferenceResoulutionList(List<YangNode> nestedReferenceResoulutionList) {
this.nestedReferenceResoulutionList = nestedReferenceResoulutionList;
......@@ -577,7 +577,7 @@ public class YangModule extends YangNode
/**
* Set list of nested reference's which requires resolution.
*
* @param nestedReference nested reference which requires resolution.
* @param nestedReference nested reference which requires resolution
*/
public void addToNestedReferenceResoulutionList(YangNode nestedReference) {
if (getNestedReferenceResoulutionList() == null) {
......@@ -589,7 +589,7 @@ public class YangModule extends YangNode
/**
* Returns the type of the parsed data.
*
* @return returns MODULE_DATA.
* @return returns MODULE_DATA
*/
@Override
public YangConstructType getYangConstructType() {
......@@ -625,18 +625,21 @@ public class YangModule extends YangNode
/**
* Generates java code for module.
*
* @throws IOException when fails to generate the source files.
* @param codeGenDir code generation directory
* @throws IOException when fails to generate the source files
*/
@Override
public void generateJavaCodeEntry() throws IOException {
public void generateJavaCodeEntry(String codeGenDir) throws IOException {
String modPkg = JavaIdentifierSyntax.getRootPackage(getVersion(), getNameSpace().getUri(),
getRevision().getRevDate());
modPkg = JavaIdentifierSyntax.getCamelCase(modPkg);
CachedFileHandle handle = null;
try {
FileSystemUtil.createPackage(UtilConstants.YANG_GEN_DIR + modPkg, getName());
FileSystemUtil.createPackage(codeGenDir + modPkg, getName());
handle = FileSystemUtil.createSourceFiles(modPkg, getName(),
GeneratedFileType.GENERATE_INTERFACE_WITH_BUILDER);
handle.setCodeGenFilePath(codeGenDir);
} catch (IOException e) {
throw new IOException("Failed to create the source files.");
}
......@@ -680,8 +683,8 @@ public class YangModule extends YangNode
/**
* Add a type to resolve the nested references.
*
* @param node grouping or typedef node which needs to be resolved.
* @throws DataModelException data model exception.
* @param node grouping or typedef node which needs to be resolved
* @throws DataModelException data model exception
*/
public static void addToResolveList(YangNode node) throws DataModelException {
/* get the module node to add maintain the list of nested reference */
......
......@@ -470,9 +470,11 @@ public class YangSubModule extends YangNode
/**
* Generates java code for sub-module.
*
* @param codeGenDir code generation directory.
*/
@Override
public void generateJavaCodeEntry() {
public void generateJavaCodeEntry(String codeGenDir) {
// TODO Auto-generated method stub
}
......
......@@ -47,7 +47,7 @@ import org.onosproject.yangutils.utils.YangConstructType;
/**
* Maintains the data type information.
*
* @param <T> YANG data type info.
* @param <T> YANG data type info
*/
public class YangType<T> implements Parsable {
......@@ -57,6 +57,11 @@ public class YangType<T> implements Parsable {
private String dataTypeName;
/**
* Java package in which the Java type is defined.
*/
private String javaPackage;
/**
* YANG data type.
*/
private YangDataTypes dataType;
......@@ -93,6 +98,24 @@ public class YangType<T> implements Parsable {
}
/**
* Get the Java package where the type is defined.
*
* @return Java package where the type is defined
*/
public String getJavaPackage() {
return javaPackage;
}
/**
* Set Java package where the type is defined.
*
* @param javaPackage Java package where the type is defined
*/
public void setJavaPackage(String javaPackage) {
this.javaPackage = javaPackage;
}
/**
* Get the type of data.
*
* @return the data type
......
......@@ -15,10 +15,16 @@
*/
package org.onosproject.yangutils.datamodel;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
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.YangConstructType;
import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
/*-
* Reference RFC 6020.
......@@ -91,6 +97,11 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
private String pkg;
/**
* Cached Java File Handle.
*/
private CachedFileHandle fileHandle;
/**
* Create a typedef node.
*/
public YangTypeDef() {
......@@ -298,20 +309,60 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
/**
* Generate java code snippet corresponding to YANG typedef.
*
* @param codeGenDir code generation directory
* @throws IOException when fails to generate files for typedef
*/
@Override
public void generateJavaCodeEntry() {
// TODO Auto-generated method stub
public void generateJavaCodeEntry(String codeGenDir) throws IOException {
YangNode parent = getParent();
String typeDefPkg = JavaIdentifierSyntax.getPackageFromParent(parent.getPackage(), parent.getName());
typeDefPkg = JavaIdentifierSyntax.getCamelCase(typeDefPkg).toLowerCase();
setPackage(typeDefPkg);
CachedFileHandle handle = null;
try {
FileSystemUtil.createPackage(codeGenDir + getPackage(), parent.getName() + UtilConstants.CHILDREN);
handle = FileSystemUtil.createSourceFiles(getPackage(), getName(),
GeneratedFileType.GENERATE_TYPEDEF_CLASS);
handle.setRelativeFilePath(getPackage().replace(".", "/"));
handle.setCodeGenFilePath(codeGenDir);
} catch (IOException e) {
throw new IOException("Failed to create the source files.");
}
setFileHandle(handle);
addAttributeInfo();
addAttributeInParent();
}
/**
* Adds current node attribute to parent file.
*/
private void addAttributeInParent() {
if (getParent() != null) {
getParent().getFileHandle().addAttributeInfo(null, getName(), false);
}
}
/**
* Adds attribute to file handle.
*/
private void addAttributeInfo() {
getFileHandle().addAttributeInfo(getDerivedType().getDataTypeExtendedInfo().getBaseType(),
JavaIdentifierSyntax.getCamelCase(getName()), false);
}
/**
* Free resource used for code generation of YANG typedef.
*
* @throws IOException when fails to generate files
*/
@Override
public void generateJavaCodeExit() {
// TODO Auto-generated method stub
public void generateJavaCodeExit() throws IOException {
getFileHandle().close();
return;
}
/**
......@@ -342,18 +393,16 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
*/
@Override
public CachedFileHandle getFileHandle() {
// TODO Auto-generated method stub
return null;
return fileHandle;
}
/**
* Set the file handle to be used used for code generation.
*
* @param fileHandle cached file handle
* @param handle cached file handle
*/
@Override
public void setFileHandle(CachedFileHandle fileHandle) {
// TODO Auto-generated method stub
public void setFileHandle(CachedFileHandle handle) {
fileHandle = handle;
}
}
......
......@@ -213,54 +213,36 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable {
// TODO auto-generated method stub, to be implemented by parser
}
/* (non-Javadoc)
* @see org.onosproject.yangutils.datamodel.YangNode#getName()
*/
@Override
public String getName() {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.onosproject.yangutils.datamodel.YangNode#setName(java.lang.String)
*/
@Override
public void setName(String name) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see org.onosproject.yangutils.translator.CodeGenerator#generateJavaCodeEntry()
*/
@Override
public void generateJavaCodeEntry() {
public void generateJavaCodeEntry(String codeGenDir) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see org.onosproject.yangutils.translator.CodeGenerator#generateJavaCodeExit()
*/
@Override
public void generateJavaCodeExit() {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see org.onosproject.yangutils.datamodel.YangNode#getPackage()
*/
@Override
public String getPackage() {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see org.onosproject.yangutils.datamodel.YangNode#setPackage(java.lang.String)
*/
@Override
public void setPackage(String pkg) {
// TODO Auto-generated method stub
......
......@@ -43,7 +43,7 @@ import org.sonatype.plexus.build.incremental.BuildContext;
/**
* ONOS YANG utility maven plugin. Goal of plugin is yang2java Execution phase
* in generate-sources requiresDependencyResolution at compile time
* in generate-sources requiresDependencyResolution at compile time.
*/
@Mojo(name = "yang2java", defaultPhase = LifecyclePhase.GENERATE_SOURCES,
requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true)
......@@ -56,6 +56,12 @@ public class YangUtilManager extends AbstractMojo {
private String yangFilesDir;
/**
* Base directory for project.
*/
@Parameter(property = "basedir", defaultValue = "${basedir}")
private String baseDir;
/**
* Output directory.
*/
@Parameter(property = "project.build.outputDirectory", required = true, defaultValue = "target/classes")
......@@ -74,13 +80,13 @@ public class YangUtilManager extends AbstractMojo {
private BuildContext context;
private YangUtilsParser yangUtilsParser = new YangUtilsParserManager();
private String baseDir;
private String searchDir;
private String codeGenDir;
/**
* Set current project.
*
* @param curProject maven project.
* @param curProject maven project
*/
public void setCurrentProject(final MavenProject curProject) {
project = curProject;
......@@ -92,7 +98,6 @@ public class YangUtilManager extends AbstractMojo {
try {
CopyrightHeader.parseCopyrightHeader();
baseDir = project.getBasedir().toString();
/**
* For deleting the generated code in previous build.
......@@ -100,6 +105,7 @@ public class YangUtilManager extends AbstractMojo {
YangIoUtils.clean(baseDir);
searchDir = baseDir + File.separator + yangFilesDir;
codeGenDir = baseDir + File.separator + UtilConstants.YANG_GEN_DIR;
List<String> yangFiles = YangFileScanner.getYangFiles(searchDir);
Iterator<String> yangFileIterator = yangFiles.iterator();
......@@ -107,7 +113,7 @@ public class YangUtilManager extends AbstractMojo {
String yangFile = yangFileIterator.next();
try {
YangNode yangNode = yangUtilsParser.getDataModel(yangFile);
JavaCodeGenerator.generateJavaCode(yangNode);
JavaCodeGenerator.generateJavaCode(yangNode, codeGenDir);
} catch (ParserException e) {
String logInfo = "Error in file: " + e.getFileName();
if (e.getLineNumber() != 0) {
......
......@@ -16,9 +16,11 @@
package org.onosproject.yangutils.translator;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.translator.tojava.utils.TempDataStoreTypes;
/**
* Cached java file handle, which supports the addition of member attributes and
......@@ -56,4 +58,43 @@ public interface CachedFileHandle {
* @return directory package path for code generation
*/
String getRelativeFilePath();
/**
* Gets base directory package path for code generation.
*
* @return directory package path for code generation
*/
String getCodeGenFilePath();
/**
* Sets base directory package path for code generation.
*
* @param path base directory path
*/
void setCodeGenFilePath(String path);
/**
* Writes specific info to a Temp file.
*
* @param data data to be stored
* @param type type of Temp data store
* @param className class name
* @param genDir generated directory
* @throws IOException when fails to create a Temp data file
*/
void setTempData(String data, TempDataStoreTypes type, String className, String genDir) throws IOException;
/**
* Get the Temp data.
*
* @param type type of Temp data store
* @param className name of the class
* @param genDir generated directory
* @return temp data
* @throws IOException when fails to read from the file
* @throws ClassNotFoundException when class is missing
* @throws FileNotFoundException when file is missing
*/
String getTempData(TempDataStoreTypes type, String className, String genDir)
throws IOException, FileNotFoundException, ClassNotFoundException;
}
......
......@@ -26,9 +26,10 @@ public interface CodeGenerator {
/**
* Traverse the schema of application and generate corresponding code.
*
* @param codeGenDir code generation directory
* @throws IOException when fails to translate the data model tree
*/
void generateJavaCodeEntry() throws IOException;
void generateJavaCodeEntry(String codeGenDir) throws IOException;
/**
* Traverse the schema of application and generate corresponding code.
......
......@@ -41,7 +41,7 @@ public class AttributeInfo {
/**
* If the added attribute has to be accessed in a fully qualified manner.
*/
private boolean isQualifiedName;
private boolean isQualifiedName = false;
/**
* The class info will be used to set the attribute type and package info
......
......@@ -16,21 +16,26 @@
package org.onosproject.yangutils.translator.tojava;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.YangTypeDef;
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.JavaFileGenerator;
import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax;
import org.onosproject.yangutils.translator.tojava.utils.TempDataStoreTypes;
import org.onosproject.yangutils.utils.UtilConstants;
import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
/**
* Maintain the information about the java file to be generated.
......@@ -40,6 +45,17 @@ public class CachedJavaFileHandle implements CachedFileHandle {
private static final int MAX_CACHABLE_ATTR = 64;
private static final String JAVA_FILE_EXTENSION = ".java";
private static final String TEMP_FILE_EXTENSION = ".tmp";
private static final String GETTER_METHOD_FILE_NAME = "GetterMethod";
private static final String SETTER_METHOD_FILE_NAME = "SetterMethod";
private static final String GETTER_METHOD_IMPL_FILE_NAME = "GetterMethodImpl";
private static final String SETTER_METHOD_IMPL_FILE_NAME = "SetterMethodImpl";
private static final String CONSTRUCTOR_FILE_NAME = "Constructor";
private static final String ATTRIBUTE_FILE_NAME = "Attributes";
private static final String TO_STRING_METHOD_FILE_NAME = "ToString";
private static final String HASH_CODE_METHOD_FILE_NAME = "HashCode";
private static final String EQUALS_METHOD_FILE_NAME = "Equals";
private static final String TYPE_DEF_FILE_NAME = "TypeDef";
private static final String TEMP_FOLDER_NAME_SUFIX = "-Temp";
/**
* The type(s) of java source file(s) to be generated when the cached file
......@@ -69,9 +85,9 @@ public class CachedJavaFileHandle implements CachedFileHandle {
private String relativeFilePath;
/**
* Typedef Info.
* File generation base directory path.
*/
private YangTypeDef typedefInfo;
private String codeGenDirFilePath;
/**
* Prevent invoking default constructor.
......@@ -185,33 +201,34 @@ public class CachedJavaFileHandle implements CachedFileHandle {
attributeList = attrList;
}
/**
* Set the package relative path.
*
* @param path package relative path
*/
@Override
public void setRelativeFilePath(String path) {
relativeFilePath = path;
}
/**
* Get the package relative path.
*
* @return package relative path
*/
@Override
public String getRelativeFilePath() {
return relativeFilePath;
}
@Override
public String getCodeGenFilePath() {
return codeGenDirFilePath;
}
@Override
public void setCodeGenFilePath(String path) {
codeGenDirFilePath = path;
}
/**
* Flush the cached attribute list to the corresponding temporary file.
*/
private void flushCacheAttrToTempFile() {
for (AttributeInfo attr : getCachedAttributeList()) {
JavaFileGenerator.parseAttributeInfo(attr, getGeneratedFileTypes(), getYangName());
JavaFileGenerator.parseAttributeInfo(attr, getGeneratedFileTypes(), getYangName(), getCodeGenFilePath() +
getRelativeFilePath().replace(UtilConstants.PERIOD, UtilConstants.SLASH), this);
}
/*
......@@ -220,14 +237,6 @@ public class CachedJavaFileHandle implements CachedFileHandle {
getCachedAttributeList().clear();
}
/**
* Add a new attribute to the file(s).
*
* @param attrType data type of the added attribute
* @param name name of the attribute
* @param isListAttr if the current added attribute needs to be maintained
* in a list
*/
@Override
public void addAttributeInfo(YangType<?> attrType, String name, boolean isListAttr) {
/* YANG name is mapped to java name */
......@@ -256,16 +265,29 @@ public class CachedJavaFileHandle implements CachedFileHandle {
} else {
importInfo.setClassInfo(JavaIdentifierSyntax.getCaptialCase(name));
importInfo.setPkgInfo(getRelativeFilePath().replace('/', '.')
+ "." + getYangName());
+ "." + getYangName().toLowerCase());
isImport = true;
}
newAttr.setQualifiedName(false);
if (isImport) {
boolean isNewImport = addImportInfo(importInfo);
if (!isNewImport) {
addImportInfo(importInfo);
}
if (isListAttr) {
ImportInfo listImportInfo = new ImportInfo();
listImportInfo.setPkgInfo(UtilConstants.COLLECTION_IMPORTS);
listImportInfo.setClassInfo(UtilConstants.LIST);
addImportInfo(listImportInfo);
}
/**
* If two classes with different packages have same class info for import than use qualified name.
*/
for (ImportInfo imports : getImportSet()) {
if (imports.getClassInfo().equals(importInfo.getClassInfo())
&& !imports.getPkgInfo().equals(importInfo.getPkgInfo())) {
newAttr.setQualifiedName(true);
}
}
......@@ -280,9 +302,6 @@ public class CachedJavaFileHandle implements CachedFileHandle {
getCachedAttributeList().add(newAttr);
}
/**
* Flushes the cached contents to the target file, frees used resources.
*/
@Override
public void close() throws IOException {
......@@ -298,17 +317,18 @@ public class CachedJavaFileHandle implements CachedFileHandle {
* JavaCodeSnippetGen.getFileHeaderComment
*/
List<String> imports = new LinkedList<>();
List<String> imports = new ArrayList<>();
String importString;
for (ImportInfo importInfo : getImportSet()) {
importString = "";
for (ImportInfo importInfo : new ArrayList<ImportInfo>(getImportSet())) {
importString = UtilConstants.IMPORT;
if (importInfo.getPkgInfo() != null) {
importString = importString + importInfo.getPkgInfo() + ".";
}
importString = importString + importInfo.getClassInfo();
importString = importString + importInfo.getClassInfo() + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE;
imports.add(importString);
}
java.util.Collections.sort(imports);
/**
* Start generation of files.
......@@ -320,25 +340,28 @@ public class CachedJavaFileHandle implements CachedFileHandle {
* Create interface file.
*/
String interfaceFileName = className;
File interfaceFile = JavaFileGenerator.getFileObject(path, interfaceFileName, JAVA_FILE_EXTENSION);
File interfaceFile = JavaFileGenerator.getFileObject(path, interfaceFileName, JAVA_FILE_EXTENSION, this);
interfaceFile = JavaFileGenerator.generateInterfaceFile(interfaceFile, className, imports,
getCachedAttributeList(), path.replace('/', '.'));
getCachedAttributeList(), path.replace('/', '.'), this);
/**
* Create temp builder interface file.
*/
String builderInterfaceFileName = className + UtilConstants.BUILDER + UtilConstants.INTERFACE;
File builderInterfaceFile = JavaFileGenerator.getFileObject(path, builderInterfaceFileName,
TEMP_FILE_EXTENSION);
TEMP_FILE_EXTENSION, this);
builderInterfaceFile = JavaFileGenerator.generateBuilderInterfaceFile(builderInterfaceFile, className,
path.replace('/', '.'), getCachedAttributeList());
path.replace('/', '.'), getCachedAttributeList(), this);
/**
* Append builder interface file to interface file and close it.
*/
JavaFileGenerator.appendFileContents(builderInterfaceFile, interfaceFile);
JavaFileGenerator.insert(interfaceFile,
JavaFileGenerator.closeFile(GeneratedFileType.INTERFACE_MASK, interfaceFileName));
/**
* Close file handle for interface files.
*/
JavaFileGenerator.closeFileHandles(builderInterfaceFile);
JavaFileGenerator.closeFileHandles(interfaceFile);
/**
* Remove temp files.
......@@ -346,6 +369,10 @@ public class CachedJavaFileHandle implements CachedFileHandle {
JavaFileGenerator.clean(builderInterfaceFile);
}
imports.add(UtilConstants.MORE_OBJECT_IMPORT);
imports.add(UtilConstants.JAVA_UTIL_OBJECTS_IMPORT);
java.util.Collections.sort(imports);
if ((fileType & GeneratedFileType.BUILDER_CLASS_MASK) != 0
|| fileType == GeneratedFileType.GENERATE_INTERFACE_WITH_BUILDER) {
......@@ -353,19 +380,17 @@ public class CachedJavaFileHandle implements CachedFileHandle {
* Create builder class file.
*/
String builderFileName = className + UtilConstants.BUILDER;
File builderFile = JavaFileGenerator.getFileObject(path, builderFileName, JAVA_FILE_EXTENSION);
File builderFile = JavaFileGenerator.getFileObject(path, builderFileName, JAVA_FILE_EXTENSION, this);
builderFile = JavaFileGenerator.generateBuilderClassFile(builderFile, className, imports,
path.replace('/', '.'), getCachedAttributeList());
path.replace('/', '.'), getCachedAttributeList(), this);
/**
* Create temp impl class file.
*/
String implFileName = className + UtilConstants.IMPL;
File implTempFile = JavaFileGenerator.getFileObject(path, implFileName, TEMP_FILE_EXTENSION);
File implTempFile = JavaFileGenerator.getFileObject(path, implFileName, TEMP_FILE_EXTENSION, this);
implTempFile = JavaFileGenerator.generateImplClassFile(implTempFile, className,
path.replace('/', '.'), getCachedAttributeList());
path.replace('/', '.'), getCachedAttributeList(), this);
/**
* Append impl class to builder class and close it.
*/
......@@ -374,17 +399,210 @@ public class CachedJavaFileHandle implements CachedFileHandle {
JavaFileGenerator.closeFile(GeneratedFileType.BUILDER_CLASS_MASK, builderFileName));
/**
* Close file handle for classes files.
*/
JavaFileGenerator.closeFileHandles(implTempFile);
JavaFileGenerator.closeFileHandles(builderFile);
/**
* Remove temp files.
*/
JavaFileGenerator.clean(implTempFile);
}
if ((fileType & GeneratedFileType.GENERATE_TYPEDEF_CLASS) != 0) {
/**
* Create builder class file.
*/
String typeDefFileName = className;
File typeDefFile = JavaFileGenerator.getFileObject(path, typeDefFileName, JAVA_FILE_EXTENSION, this);
typeDefFile = JavaFileGenerator.generateTypeDefClassFile(typeDefFile, className, imports,
path.replace('/', '.'), getCachedAttributeList(), this);
JavaFileGenerator.insert(typeDefFile,
JavaFileGenerator.closeFile(GeneratedFileType.GENERATE_TYPEDEF_CLASS, typeDefFileName));
/**
* Close file handle for classes files.
*/
JavaFileGenerator.closeFileHandles(typeDefFile);
}
closeTempDataFileHandles(className, getCodeGenFilePath() + getRelativeFilePath());
JavaFileGenerator
.cleanTempFiles(new File(getCodeGenFilePath() + getRelativeFilePath() + File.separator + className
+ TEMP_FOLDER_NAME_SUFIX));
}
public YangTypeDef getTypedefInfo() {
return typedefInfo;
@Override
public void setTempData(String data, TempDataStoreTypes type, String className, String genDir)
throws IOException {
String fileName = "";
if (type.equals(TempDataStoreTypes.ATTRIBUTE)) {
fileName = ATTRIBUTE_FILE_NAME;
} else if (type.equals(TempDataStoreTypes.GETTER_METHODS)) {
fileName = GETTER_METHOD_FILE_NAME;
} else if (type.equals(TempDataStoreTypes.GETTER_METHODS_IMPL)) {
fileName = GETTER_METHOD_IMPL_FILE_NAME;
} else if (type.equals(TempDataStoreTypes.SETTER_METHODS)) {
fileName = SETTER_METHOD_FILE_NAME;
} else if (type.equals(TempDataStoreTypes.SETTER_METHODS_IMPL)) {
fileName = SETTER_METHOD_IMPL_FILE_NAME;
} else if (type.equals(TempDataStoreTypes.TYPE_DEF)) {
fileName = TYPE_DEF_FILE_NAME;
} else if (type.equals(TempDataStoreTypes.TO_STRING)) {
fileName = TO_STRING_METHOD_FILE_NAME;
} else if (type.equals(TempDataStoreTypes.HASH_CODE)) {
fileName = HASH_CODE_METHOD_FILE_NAME;
} else if (type.equals(TempDataStoreTypes.EQUALS)) {
fileName = EQUALS_METHOD_FILE_NAME;
} else {
fileName = CONSTRUCTOR_FILE_NAME;
}
String path = genDir.replace(UtilConstants.PERIOD, UtilConstants.SLASH)
+ File.separator + className
+ TEMP_FOLDER_NAME_SUFIX + File.separator;
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(path + fileName + TEMP_FILE_EXTENSION);
try {
if (!file.exists()) {
file.createNewFile();
JavaFileGenerator.insert(file, data);
} else {
JavaFileGenerator.insert(file, data);
}
} catch (IOException ex) {
throw new IOException("failed to write in temp file.");
}
}
@Override
public String getTempData(TempDataStoreTypes type, String className, String genDir)
throws IOException, FileNotFoundException, ClassNotFoundException {
String fileName = "";
if (type.equals(TempDataStoreTypes.ATTRIBUTE)) {
fileName = ATTRIBUTE_FILE_NAME;
} else if (type.equals(TempDataStoreTypes.GETTER_METHODS)) {
fileName = GETTER_METHOD_FILE_NAME;
} else if (type.equals(TempDataStoreTypes.GETTER_METHODS_IMPL)) {
fileName = GETTER_METHOD_IMPL_FILE_NAME;
} else if (type.equals(TempDataStoreTypes.SETTER_METHODS)) {
fileName = SETTER_METHOD_FILE_NAME;
} else if (type.equals(TempDataStoreTypes.SETTER_METHODS_IMPL)) {
fileName = SETTER_METHOD_IMPL_FILE_NAME;
} else if (type.equals(TempDataStoreTypes.TYPE_DEF)) {
fileName = TYPE_DEF_FILE_NAME;
} else if (type.equals(TempDataStoreTypes.TO_STRING)) {
fileName = TO_STRING_METHOD_FILE_NAME;
} else if (type.equals(TempDataStoreTypes.HASH_CODE)) {
fileName = HASH_CODE_METHOD_FILE_NAME;
} else if (type.equals(TempDataStoreTypes.EQUALS)) {
fileName = EQUALS_METHOD_FILE_NAME;
} else {
fileName = CONSTRUCTOR_FILE_NAME;
}
String path = genDir.replace(UtilConstants.PERIOD, UtilConstants.SLASH)
+ File.separator + className + TEMP_FOLDER_NAME_SUFIX + File.separator;
try {
return readFile(path + fileName + TEMP_FILE_EXTENSION);
} catch (FileNotFoundException e) {
throw new FileNotFoundException("No such file or directory.");
}
}
/**
* Reads file and convert it to string.
*
* @param toAppend file to be converted
* @return string of file
* @throws IOException when fails to convert to string
*/
private static String readFile(String toAppend) throws IOException {
BufferedReader bufferReader = new BufferedReader(new FileReader(toAppend));
try {
StringBuilder stringBuilder = new StringBuilder();
String line = bufferReader.readLine();
while (line != null) {
stringBuilder.append(line);
stringBuilder.append("\n");
line = bufferReader.readLine();
}
return stringBuilder.toString();
} finally {
bufferReader.close();
}
}
/**
* Closes the temp file handles.
*
* @param className class name
* @param genDir generated directory
* @throws IOException when failes to close file handle
*/
private void closeTempDataFileHandles(String className, String genDir)
throws IOException {
String path = genDir.replace(UtilConstants.PERIOD, UtilConstants.SLASH) + File.separator + className
+ TEMP_FOLDER_NAME_SUFIX + File.separator;
String fileName = "";
fileName = ATTRIBUTE_FILE_NAME;
closeTempFile(fileName, path);
fileName = GETTER_METHOD_FILE_NAME;
closeTempFile(fileName, path);
fileName = GETTER_METHOD_IMPL_FILE_NAME;
closeTempFile(fileName, path);
fileName = SETTER_METHOD_FILE_NAME;
closeTempFile(fileName, path);
fileName = SETTER_METHOD_IMPL_FILE_NAME;
closeTempFile(fileName, path);
fileName = TYPE_DEF_FILE_NAME;
closeTempFile(fileName, path);
fileName = TO_STRING_METHOD_FILE_NAME;
closeTempFile(fileName, path);
fileName = HASH_CODE_METHOD_FILE_NAME;
closeTempFile(fileName, path);
fileName = EQUALS_METHOD_FILE_NAME;
closeTempFile(fileName, path);
fileName = CONSTRUCTOR_FILE_NAME;
closeTempFile(fileName, path);
}
public void setTypedefInfo(YangTypeDef typedefInfo) {
this.typedefInfo = typedefInfo;
/**
* Closes the specific temp file.
*
* @param fileName temp file name
* @param path path
* @throws IOException when failed to close file handle
*/
private void closeTempFile(String fileName, String path) throws IOException {
File file = new File(path + fileName + TEMP_FILE_EXTENSION);
try {
if (!file.exists()) {
FileSystemUtil.updateFileHandle(file, null, true);
}
} catch (IOException ex) {
throw new IOException("failed to close the temp file handle.");
}
}
}
......
......@@ -35,16 +35,16 @@ public final class JavaCodeGenerator {
* Generate Java code files corresponding to the YANG schema.
*
* @param rootNode root node of the data model tree
* @throws IOException when fails to generate java code file the current
* node
* @param codeGenDir code generation directory
* @throws IOException when fails to generate java code file the current node
*/
public static void generateJavaCode(YangNode rootNode) throws IOException {
public static void generateJavaCode(YangNode rootNode, String codeGenDir) throws IOException {
YangNode curNode = rootNode;
TraversalType curTraversal = TraversalType.ROOT;
while (!(curNode == null)) {
if (curTraversal != TraversalType.PARENT) {
curNode.generateJavaCodeEntry();
curNode.generateJavaCodeEntry(codeGenDir);
}
if (curTraversal != TraversalType.PARENT && curNode.getChild() != null) {
curTraversal = TraversalType.CHILD;
......
......@@ -79,7 +79,7 @@ public final class AttributesJavaDataType {
} else if (type.equals(YangDataTypes.INSTANCE_IDENTIFIER)) {
//TODO:INSTANCE_IDENTIFIER
} else if (type.equals(YangDataTypes.DERIVED)) {
//TODO:DERIVED
return yangType.getDataTypeName();
}
return null;
}
......
......@@ -56,6 +56,9 @@ public final class ClassDefinitionGenerator {
} else if ((genFileTypes & GeneratedFileType.BUILDER_INTERFACE_MASK) != 0) {
return getBuilderInterfaceDefinition(yangName);
} else if ((genFileTypes & GeneratedFileType.GENERATE_TYPEDEF_CLASS) != 0) {
return getTypeDefClassDefinition(yangName);
}
return null;
}
......@@ -94,7 +97,7 @@ public final class ClassDefinitionGenerator {
return UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.CLASS + UtilConstants.SPACE + yangName
+ UtilConstants.BUILDER + UtilConstants.SPACE + UtilConstants.IMPLEMENTS + UtilConstants.SPACE
+ yangName + UtilConstants.PERIOD + UtilConstants.BUILDER + UtilConstants.SPACE
+ yangName + UtilConstants.PERIOD + yangName + UtilConstants.BUILDER + UtilConstants.SPACE
+ UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE;
}
......@@ -108,8 +111,22 @@ public final class ClassDefinitionGenerator {
return UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.FINAL + UtilConstants.SPACE
+ UtilConstants.CLASS + UtilConstants.SPACE + yangName + UtilConstants.IMPL + UtilConstants.SPACE
+ UtilConstants.IMPLEMENTS + UtilConstants.SPACE + yangName + UtilConstants.OPEN_CURLY_BRACKET
+ UtilConstants.IMPLEMENTS + UtilConstants.SPACE + yangName + UtilConstants.SPACE
+ UtilConstants.OPEN_CURLY_BRACKET
+ UtilConstants.SPACE + UtilConstants.NEW_LINE;
}
/**
* Returns typeDef file class definition.
*
* @param yangName file name
* @return definition
*/
private static String getTypeDefClassDefinition(String yangName) {
return UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.FINAL + UtilConstants.SPACE
+ UtilConstants.CLASS + UtilConstants.SPACE + yangName + UtilConstants.SPACE
+ UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.SPACE + UtilConstants.NEW_LINE;
}
}
......
......@@ -78,25 +78,37 @@ public final class JavaCodeSnippetGen {
* @param javaAttributeTypePkg Package of the attribute type
* @param javaAttributeType java attribute type
* @param javaAttributeName name of the attribute
* @param isList is list attribute
* @return the textual java code for attribute definition in class
*/
public static String getJavaAttributeDefination(String javaAttributeTypePkg, String javaAttributeType,
String javaAttributeName) {
String javaAttributeName, boolean isList) {
String attributeDefination = UtilConstants.PRIVATE
+ UtilConstants.SPACE;
if (javaAttributeTypePkg != null) {
attributeDefination = attributeDefination
+ javaAttributeTypePkg + ".";
}
if (!isList) {
if (javaAttributeTypePkg != null) {
attributeDefination = attributeDefination
+ javaAttributeTypePkg + ".";
}
attributeDefination = attributeDefination
+ javaAttributeType
+ UtilConstants.SPACE
+ javaAttributeName
+ UtilConstants.SEMI_COLAN;
attributeDefination = attributeDefination
+ javaAttributeType
+ UtilConstants.SPACE
+ javaAttributeName
+ UtilConstants.SEMI_COLAN;
} else {
attributeDefination = attributeDefination + UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET;
if (javaAttributeTypePkg != null) {
attributeDefination = attributeDefination
+ javaAttributeTypePkg + ".";
}
attributeDefination = attributeDefination
+ javaAttributeType + UtilConstants.DIAMOND_CLOSE_BRACKET + UtilConstants.SPACE
+ javaAttributeName + UtilConstants.SEMI_COLAN;
}
return attributeDefination;
}
......@@ -126,6 +138,9 @@ public final class JavaCodeSnippetGen {
} else if ((genFileTypes & GeneratedFileType.BUILDER_CLASS_MASK) != 0) {
return UtilConstants.CLOSE_CURLY_BRACKET;
} else if ((genFileTypes & GeneratedFileType.GENERATE_TYPEDEF_CLASS) != 0) {
return UtilConstants.CLOSE_CURLY_BRACKET;
}
return null;
}
......
......@@ -16,26 +16,28 @@
package org.onosproject.yangutils.translator.tojava.utils;
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.GENERATE_TYPEDEF_CLASS;
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;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.onosproject.yangutils.translator.CachedFileHandle;
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.JavaDocGen.JavaDocType;
import org.onosproject.yangutils.utils.io.impl.TempDataStore;
import org.onosproject.yangutils.utils.io.impl.TempDataStore.TempDataStoreType;
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.
*/
......@@ -55,10 +57,11 @@ public final class JavaFileGenerator {
* @param fileName file name
* @param filePath file package path
* @param extension file extension
* @param handle cached file handle
* @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);
public static File getFileObject(String filePath, String fileName, String extension, CachedFileHandle handle) {
return new File(handle.getCodeGenFilePath() + filePath + File.separator + fileName + extension);
}
/**
......@@ -69,17 +72,18 @@ public final class JavaFileGenerator {
* @param imports imports for the file
* @param attrList attribute info
* @param pkg generated file package
* @param handle cached file handle
* @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 {
List<AttributeInfo> attrList, String pkg, CachedFileHandle handle) throws IOException {
String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
initiateFile(file, className, INTERFACE_MASK, imports, pkg);
List<String> methods;
List<String> methods = new ArrayList<>();
try {
methods = TempDataStore.getTempData(TempDataStoreType.GETTER_METHODS, className);
methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS, className, path));
} 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.");
......@@ -101,17 +105,19 @@ public final class JavaFileGenerator {
* @param className class name
* @param pkg generated file package
* @param attrList attribute info
* @param handle cached file handle
* @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 {
List<AttributeInfo> attrList, CachedFileHandle handle) throws IOException {
String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
initiateFile(file, className, BUILDER_INTERFACE_MASK, null, pkg);
List<String> methods;
List<String> methods = new ArrayList<>();
try {
methods = TempDataStore.getTempData(TempDataStoreType.BUILDER_INTERFACE_METHODS,
className + UtilConstants.BUILDER + UtilConstants.INTERFACE);
methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS, className, path));
methods.add(handle.getTempData(TempDataStoreTypes.SETTER_METHODS, className, path));
} 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.");
......@@ -126,7 +132,7 @@ public final class JavaFileGenerator {
* Add getters and setters in builder interface.
*/
for (String method : methods) {
appendMethod(file, UtilConstants.FOUR_SPACE_INDENTATION + method + UtilConstants.NEW_LINE);
appendMethod(file, UtilConstants.FOUR_SPACE_INDENTATION + method);
}
insert(file, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE);
......@@ -141,20 +147,21 @@ public final class JavaFileGenerator {
* @param imports imports for the file
* @param pkg generated file package
* @param attrList attribute info
* @param handle cached file handle
* @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 {
List<AttributeInfo> attrList, CachedFileHandle handle) throws IOException {
String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
initiateFile(file, className, BUILDER_CLASS_MASK, imports, pkg);
/**
* Add attribute strings.
*/
List<String> attributes;
List<String> attributes = new ArrayList<>();
try {
attributes = TempDataStore.getTempData(TempDataStoreType.ATTRIBUTE, className);
attributes.add(handle.getTempData(TempDataStoreTypes.ATTRIBUTE, className, path));
} 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.");
......@@ -165,11 +172,11 @@ public final class JavaFileGenerator {
for (String attribute : attributes) {
insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
}
insert(file, UtilConstants.NEW_LINE);
List<String> methods;
List<String> methods = new ArrayList<>();
try {
methods = TempDataStore.getTempData(TempDataStoreType.BUILDER_METHODS, className + UtilConstants.BUILDER);
methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS_IMPL, className, path));
methods.add(handle.getTempData(TempDataStoreTypes.SETTER_METHODS_IMPL, className, path));
} 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.");
......@@ -179,7 +186,8 @@ public final class JavaFileGenerator {
* Add default constructor and build method impl.
*/
methods.add(UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE
+ MethodsGenerator.getDefaultConstructorString(BUILDER_CLASS_MASK, className));
+ MethodsGenerator.getDefaultConstructorString(className + UtilConstants.BUILDER,
UtilConstants.PUBLIC));
methods.add(MethodsGenerator.getBuildString(className));
/**
......@@ -198,17 +206,19 @@ public final class JavaFileGenerator {
* @param className class name
* @param pkg generated file package
* @param attrList attribute's info
* @param handle cached file handle
* @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 {
public static File generateImplClassFile(File file, String className, String pkg, List<AttributeInfo> attrList,
CachedFileHandle handle)
throws IOException {
String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
initiateFile(file, className, IMPL_CLASS_MASK, null, path);
initiateFile(file, className, IMPL_CLASS_MASK, null, pkg);
List<String> attributes;
List<String> attributes = new ArrayList<>();
try {
attributes = TempDataStore.getTempData(TempDataStoreType.ATTRIBUTE, className);
attributes.add(handle.getTempData(TempDataStoreTypes.ATTRIBUTE, className, path));
} 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.");
......@@ -220,24 +230,34 @@ public final class JavaFileGenerator {
for (String attribute : attributes) {
insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
}
insert(file, UtilConstants.NEW_LINE);
List<String> methods;
List<String> methods = new ArrayList<>();
try {
methods = TempDataStore.getTempData(TempDataStoreType.IMPL_METHODS, className + UtilConstants.IMPL);
methods.add(handle.getTempData(TempDataStoreTypes.GETTER_METHODS_IMPL, className, path));
methods.add(getConstructorString(className)
+ handle.getTempData(TempDataStoreTypes.CONSTRUCTOR, className, path)
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET);
methods.add(MethodsGenerator.getHashCodeMethodClose(MethodsGenerator.getHashCodeMethodOpen()
+ handle.getTempData(TempDataStoreTypes.HASH_CODE, className, path).replace(UtilConstants.NEW_LINE,
"")));
methods.add(MethodsGenerator
.getEqualsMethodClose(MethodsGenerator.getEqualsMethodOpen(className + UtilConstants.IMPL)
+ handle.getTempData(TempDataStoreTypes.EQUALS, className, path)));
methods.add(MethodsGenerator.getToStringMethodOpen()
+ handle.getTempData(TempDataStoreTypes.TO_STRING, className, path)
+ MethodsGenerator.getToStringMethodClose());
} 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(IMPL_CLASS_MASK, className));
methods.add(MethodsGenerator.getConstructorString(className));
/**
* Add methods in impl class.
*/
for (String method : methods) {
......@@ -249,6 +269,58 @@ public final class JavaFileGenerator {
}
/**
* Generate class file for type def.
*
* @param file generated file
* @param className file name
* @param imports imports for file
* @param pkg package path
* @param cachedAttributeList attribute list
* @param handle cached file handle
* @return type def class file
* @throws IOException when fails to generate class file
*/
public static File generateTypeDefClassFile(File file, String className, List<String> imports,
String pkg, List<AttributeInfo> cachedAttributeList, CachedFileHandle handle) throws IOException {
String path = handle.getCodeGenFilePath() + pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
initiateFile(file, className, GENERATE_TYPEDEF_CLASS, imports, pkg);
List<String> typeDef = new ArrayList<>();
try {
typeDef.add(handle.getTempData(TempDataStoreTypes.TYPE_DEF, className, path));
} 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 : typeDef) {
insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + attribute);
}
return file;
}
/**
* Returns constructor string for impl class.
*
* @param yangName class name
* @return constructor string
*/
private static String getConstructorString(String yangName) {
String builderAttribute = yangName.substring(0, 1).toLowerCase() + yangName.substring(1);
String javadoc = MethodsGenerator.getConstructorString(yangName);
String constructor = UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
+ yangName + UtilConstants.IMPL + UtilConstants.OPEN_PARENTHESIS + yangName + UtilConstants.BUILDER
+ UtilConstants.SPACE + builderAttribute + UtilConstants.OBJECT + UtilConstants.CLOSE_PARENTHESIS
+ UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE;
return javadoc + constructor;
}
/**
* Initiate generation of file based on generated file type.
*
* @param file generated file
......@@ -288,7 +360,6 @@ public final class JavaFileGenerator {
*
* @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);
......@@ -309,17 +380,30 @@ public final class JavaFileGenerator {
* 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
* @param path file path
* @param handle cached file handle
*/
public static void parseAttributeInfo(AttributeInfo attr, int genFileType, String className) {
public static void parseAttributeInfo(AttributeInfo attr, int genFileType, String className, String path,
CachedFileHandle handle) {
String attrString = "";
String builderInterfaceMethodString = "";
String builderClassMethodString = "";
String implClassMethodString = "";
String getterString = "";
String getterImplString = "";
String setterString = "";
String setterImplString = "";
String constructorString = "";
String typeDefString = "";
String toString = "";
String hashCodeString = "";
String equalsString = "";
className = JavaIdentifierSyntax.getCaptialCase(className);
try {
......@@ -327,33 +411,124 @@ public final class JavaFileGenerator {
* Get the attribute definition and save attributes to temporary
* file.
*/
attrString = JavaCodeSnippetGen.getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(),
attr.getImportInfo().getClassInfo(),
attr.getAttributeName());
TempDataStore.setTempData(attrString, TempDataStore.TempDataStoreType.ATTRIBUTE, className);
boolean isList = attr.isListAttr();
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
if (attr.isQualifiedName()) {
attrString = JavaCodeSnippetGen.getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(),
attr.getImportInfo().getClassInfo(),
attributeName, attr.isListAttr());
} else {
attrString = JavaCodeSnippetGen.getJavaAttributeDefination(null, attr.getImportInfo().getClassInfo(),
attributeName, attr.isListAttr());
}
handle.setTempData(attrString + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION,
TempDataStoreTypes.ATTRIBUTE, className,
path);
if ((genFileType & INTERFACE_MASK) != 0) {
getterString = MethodsGenerator.getGetterString(attr);
TempDataStore.setTempData(getterString, TempDataStore.TempDataStoreType.GETTER_METHODS, className);
handle.setTempData(getterString + UtilConstants.NEW_LINE,
TempDataStoreTypes.GETTER_METHODS,
className,
path);
}
if ((genFileType & BUILDER_INTERFACE_MASK) != 0) {
builderInterfaceMethodString = MethodsGenerator.parseBuilderInterfaceMethodString(attr, className);
TempDataStore.setTempData(builderInterfaceMethodString,
TempDataStore.TempDataStoreType.BUILDER_INTERFACE_METHODS,
className + UtilConstants.BUILDER + UtilConstants.INTERFACE);
setterString = MethodsGenerator.getSetterString(attr, className);
handle.setTempData(setterString + UtilConstants.NEW_LINE,
TempDataStoreTypes.SETTER_METHODS,
className,
path);
}
if ((genFileType & BUILDER_CLASS_MASK) != 0) {
builderClassMethodString = MethodsGenerator.parseBuilderMethodString(attr, className);
TempDataStore.setTempData(builderClassMethodString, TempDataStore.TempDataStoreType.BUILDER_METHODS,
className + UtilConstants.BUILDER);
getterImplString = MethodsGenerator.getGetterForClass(attr);
handle.setTempData(
MethodsGenerator.getOverRideString() + getterImplString + UtilConstants.NEW_LINE,
TempDataStoreTypes.GETTER_METHODS_IMPL, className,
path);
setterImplString = MethodsGenerator.getSetterForClass(attr, className);
handle.setTempData(
MethodsGenerator.getOverRideString() + setterImplString + UtilConstants.NEW_LINE,
TempDataStoreTypes.SETTER_METHODS_IMPL, className,
path);
}
if ((genFileType & IMPL_CLASS_MASK) != 0) {
implClassMethodString = MethodsGenerator.parseImplMethodString(attr);
TempDataStore.setTempData(implClassMethodString, TempDataStore.TempDataStoreType.IMPL_METHODS,
className + UtilConstants.IMPL);
constructorString = MethodsGenerator.getConstructor(className, attr);
handle.setTempData(constructorString, TempDataStoreTypes.CONSTRUCTOR, className,
path);
hashCodeString = MethodsGenerator.getHashCodeMethod(attr);
handle.setTempData(hashCodeString + UtilConstants.NEW_LINE,
TempDataStoreTypes.HASH_CODE,
className,
path);
equalsString = MethodsGenerator.getEqualsMethod(attr);
handle.setTempData(equalsString + UtilConstants.NEW_LINE,
TempDataStoreTypes.EQUALS,
className,
path);
toString = MethodsGenerator.getToStringMethod(attr);
handle.setTempData(toString + UtilConstants.NEW_LINE,
TempDataStoreTypes.TO_STRING,
className,
path);
}
if ((genFileType & GENERATE_TYPEDEF_CLASS) != 0) {
if (attr.isQualifiedName()) {
typeDefString = JavaCodeSnippetGen.getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(),
attr.getImportInfo().getClassInfo(),
attributeName, attr.isListAttr()) + UtilConstants.NEW_LINE;
} else {
typeDefString = JavaCodeSnippetGen.getJavaAttributeDefination(null,
attr.getImportInfo().getClassInfo(),
attributeName, attr.isListAttr()) + UtilConstants.NEW_LINE;
}
typeDefString = typeDefString + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.JAVA_DOC_FIRST_LINE;
typeDefString = typeDefString
+ MethodsGenerator.getDefaultConstructorString(className, UtilConstants.PRIVATE)
+ UtilConstants.NEW_LINE;
typeDefString = typeDefString
+ JavaDocGen.getJavaDoc(JavaDocType.TYPE_DEF_CONSTRUCTOR, className, isList)
+ MethodsGenerator.getTypeDefConstructor(attr, className)
+ UtilConstants.NEW_LINE;
typeDefString = typeDefString + JavaDocGen.getJavaDoc(JavaDocType.OF, className, isList)
+ MethodsGenerator.getOfMethod(className, attr) + UtilConstants.NEW_LINE;
typeDefString = typeDefString + JavaDocGen.getJavaDoc(JavaDocType.GETTER, className, isList)
+ MethodsGenerator.getGetterForClass(attr) + UtilConstants.NEW_LINE;
typeDefString = typeDefString + JavaDocGen.getJavaDoc(JavaDocType.TYPE_DEF_SETTER, className, isList)
+ MethodsGenerator.getSetterForTypeDefClass(attr)
+ UtilConstants.NEW_LINE;
hashCodeString = hashCodeString + MethodsGenerator.getHashCodeMethodOpen()
+ MethodsGenerator.getHashCodeMethod(attr).replace(UtilConstants.NEW_LINE, "");
hashCodeString = MethodsGenerator.getHashCodeMethodClose(hashCodeString) + UtilConstants.NEW_LINE;
equalsString = equalsString + MethodsGenerator.getEqualsMethodOpen(className) + UtilConstants.NEW_LINE
+ MethodsGenerator.getEqualsMethod(attr);
equalsString = MethodsGenerator.getEqualsMethodClose(equalsString) + UtilConstants.NEW_LINE;
toString = toString + MethodsGenerator.getToStringMethodOpen()
+ MethodsGenerator.getToStringMethod(attr) + UtilConstants.NEW_LINE
+ MethodsGenerator.getToStringMethodClose()
+ UtilConstants.NEW_LINE;
typeDefString = typeDefString + hashCodeString + equalsString + toString;
handle.setTempData(typeDefString, TempDataStoreTypes.TYPE_DEF, className,
path);
}
} catch (IOException e) {
log.info("Failed to set data for " + attr.getAttributeName() + " in temp data files.");
......@@ -368,18 +543,34 @@ public final class JavaFileGenerator {
* @param fileName generated file name
* @param type generated file type
* @param pkg generated file package
* @param importsList list of java imports
* @param importsList list of java imports.
* @throws IOException when fails to append contents
*/
private static void appendContents(File file, String fileName, int type, List<String> importsList,
String pkg) throws IOException {
if (pkg.contains(UtilConstants.YANG_GEN_DIR)) {
String[] strArray = pkg.split(UtilConstants.YANG_GEN_DIR);
pkg = strArray[1].replace(UtilConstants.SLASH, UtilConstants.PERIOD);
}
if ((type & IMPL_CLASS_MASK) != 0) {
write(file, fileName, type, JavaDocType.IMPL_CLASS);
} else if ((type & BUILDER_INTERFACE_MASK) != 0) {
write(file, fileName, type, JavaDocType.BUILDER_INTERFACE);
} else if ((type & GENERATE_TYPEDEF_CLASS) != 0) {
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);
}
write(file, fileName, type, JavaDocType.IMPL_CLASS);
} else {
if ((type & INTERFACE_MASK) != 0) {
......@@ -420,7 +611,7 @@ public final class JavaFileGenerator {
private static void write(File file, String fileName, int genType, JavaDocGen.JavaDocType javaDocType)
throws IOException {
insert(file, JavaDocGen.getJavaDoc(javaDocType, fileName));
insert(file, JavaDocGen.getJavaDoc(javaDocType, fileName, false));
insert(file, JavaCodeSnippetGen.getJavaClassDefStart(genType, fileName));
}
......@@ -433,13 +624,27 @@ public final class JavaFileGenerator {
*/
public static void insert(File file, String data) throws IOException {
try {
FileSystemUtil.insertStringInFile(file, data);
FileSystemUtil.updateFileHandle(file, data, false);
} catch (IOException e) {
throw new IOException("Failed to insert in " + file + "file");
}
}
/**
* Closes the files handle for generate files.
*
* @param file generate files
* @throws IOException when failed to close the file handle
*/
public static void closeFileHandles(File file) throws IOException {
try {
FileSystemUtil.updateFileHandle(file, null, true);
} catch (IOException e) {
throw new IOException("Failed to close file handle for " + file + "file");
}
}
/**
* Removes temp files.
*
* @param file file to be removed
......@@ -449,4 +654,14 @@ public final class JavaFileGenerator {
file.delete();
}
}
/**
* Removes temp files.
*
* @param tempDir temp directory
* @throws IOException when fails to delete the directory
*/
public static void cleanTempFiles(File tempDir) throws IOException {
FileUtils.deleteDirectory(tempDir);
}
}
......
......@@ -31,7 +31,6 @@ public final class JavaIdentifierSyntax {
private static final int INDEX_ZERO = 0;
private static final int INDEX_ONE = 1;
private static final int INDEX_TWO = 2;
private static final int INDEX_THREE = 3;
/**
* Default constructor.
......@@ -100,9 +99,7 @@ public final class JavaIdentifierSyntax {
String[] revisionArr = date.split(UtilConstants.HYPHEN);
String rev = "rev";
String year = revisionArr[INDEX_ZERO];
char[] yearBytes = year.toCharArray();
rev = rev + yearBytes[INDEX_TWO] + yearBytes[INDEX_THREE];
rev = rev + revisionArr[INDEX_ZERO];
if ((Integer.parseInt(revisionArr[INDEX_ONE]) <= MAX_MONTHS)
&& Integer.parseInt(revisionArr[INDEX_TWO]) <= MAX_DAYS) {
......
......@@ -19,6 +19,7 @@ package org.onosproject.yangutils.translator.tojava.utils;
import org.onosproject.yangutils.translator.tojava.AttributeInfo;
import org.onosproject.yangutils.utils.UtilConstants;
import org.onosproject.yangutils.utils.io.impl.JavaDocGen;
import org.onosproject.yangutils.utils.io.impl.YangIoUtils;
/**
* Generated methods for generated files based on the file type.
......@@ -32,45 +33,6 @@ public final class MethodsGenerator {
}
/**
* Returns the methods strings for builder class.
*
* @param attr attribute info
* @param className java class name
* @return method string for builder class
*/
static String parseBuilderMethodString(AttributeInfo attr, String className) {
String attrQuaifiedType = "";
if (attr.getImportInfo().getPkgInfo() != null) {
attrQuaifiedType = attr.getImportInfo().getPkgInfo() + ".";
}
attrQuaifiedType = attrQuaifiedType + attr.getImportInfo().getClassInfo();
String overrideString = UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.OVERRIDE + UtilConstants.NEW_LINE;
String getterString = getGetterForClass(attr.getAttributeName(), attrQuaifiedType);
String setterString = getSetterForClass(attr.getAttributeName(), attrQuaifiedType, className);
return overrideString + getterString + UtilConstants.NEW_LINE + overrideString + setterString;
}
/**
* Returns the methods strings for builder class.
*
* @param attr attribute info
* @return method string for builder class
*/
static String parseImplMethodString(AttributeInfo attr) {
String attrQuaifiedType = "";
if (attr.getImportInfo().getPkgInfo() != null) {
attrQuaifiedType = attr.getImportInfo().getPkgInfo() + ".";
}
attrQuaifiedType = attrQuaifiedType + attr.getImportInfo().getClassInfo();
return UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.OVERRIDE
+ UtilConstants.NEW_LINE + getGetterForClass(attr.getAttributeName(), attrQuaifiedType);
}
/**
* Returns the methods strings for builder interface.
*
* @param attr attribute info
......@@ -90,7 +52,7 @@ public final class MethodsGenerator {
*/
public static String parseBuilderInterfaceBuildMethodString(String name) {
return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.BUILD, name)
return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.BUILD, name, false)
+ getBuildForInterface(name);
}
......@@ -103,14 +65,17 @@ public final class MethodsGenerator {
public static String getGetterString(AttributeInfo attr) {
String returnType = "";
if (attr.getImportInfo().getPkgInfo() != null) {
boolean isList = attr.isListAttr();
if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
returnType = attr.getImportInfo().getPkgInfo() + ".";
}
returnType = returnType + attr.getImportInfo().getClassInfo();
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.GETTER, attributeName, isList) +
return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.GETTER, attr.getAttributeName())
+ getGetterForInterface(attr.getAttributeName(), returnType)
getGetterForInterface(attributeName, returnType, attr.isListAttr())
+ UtilConstants.NEW_LINE;
}
......@@ -121,17 +86,20 @@ public final class MethodsGenerator {
* @param className java class name
* @return setter string
*/
private static String getSetterString(AttributeInfo attr, String className) {
public static String getSetterString(AttributeInfo attr, String className) {
String attrType = "";
if (attr.getImportInfo().getPkgInfo() != null) {
boolean isList = attr.isListAttr();
if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
attrType = attr.getImportInfo().getPkgInfo() + ".";
}
attrType = attrType + attr.getImportInfo().getClassInfo();
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.SETTER, attr.getAttributeName())
+ getSetterForInterface(attr.getAttributeName(), attrType, className);
return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.SETTER, attributeName, isList)
+ getSetterForInterface(attributeName, attrType, className, attr.isListAttr())
+ UtilConstants.NEW_LINE;
}
/**
......@@ -142,21 +110,75 @@ public final class MethodsGenerator {
*/
public static String getConstructorString(String name) {
return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.CONSTRUCTOR, name)
+ getConstructor(name);
return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.CONSTRUCTOR, name, false);
}
/**
* Returns default constructor method string.
*
* @param type generated file type
* @param name class name
* @param modifierType modifier type
* @return default constructor string
*/
public static String getDefaultConstructorString(int type, String name) {
public static String getDefaultConstructorString(String name, String modifierType) {
return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.DEFAULT_CONSTRUCTOR, name)
+ getDefaultConstructor(name + UtilConstants.BUILDER);
return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.DEFAULT_CONSTRUCTOR, name, false)
+ getDefaultConstructor(name, modifierType);
}
/**
* Returns default constructor method string.
*
* @param attr attribute info
* @param className class name
* @return default constructor string
*/
public static String getTypeDefConstructor(AttributeInfo attr, String className) {
String attrQuaifiedType = "";
if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
attrQuaifiedType = attr.getImportInfo().getPkgInfo() + ".";
}
attrQuaifiedType = attrQuaifiedType + attr.getImportInfo().getClassInfo();
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
if (!attr.isListAttr()) {
return getTypeDefConstructorString(attrQuaifiedType, attributeName, className);
}
String listAttr = getListString() + attrQuaifiedType + UtilConstants.DIAMOND_CLOSE_BRACKET;
return getTypeDefConstructorString(listAttr, attributeName, className);
}
/**
* Returns type def's constructor for attribute.
*
* @param type data type
* @param name attribute name
* @param className class name
* @return setter for type def's attribute
*/
private static String getTypeDefConstructorString(String type, String name, String className) {
return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
+ className + UtilConstants.OPEN_PARENTHESIS
+ type + UtilConstants.SPACE + "value" + UtilConstants.CLOSE_PARENTHESIS
+ UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE
+ UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.THIS
+ UtilConstants.PERIOD + name + UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.SPACE
+ "value" + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.CLOSE_CURLY_BRACKET;
}
/**
* Returns check not null string.
*
* @param name attribute name
* @return check not null string
*/
public static String getCheckNotNull(String name) {
return UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.CHECK_NOT_NULL_STRING
+ UtilConstants.OPEN_PARENTHESIS + name + UtilConstants.COMMA + UtilConstants.SPACE + name
+ UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE;
}
/**
......@@ -174,56 +196,171 @@ public final class MethodsGenerator {
/**
* Returns the getter method strings for class file.
*
* @param attrName name of the attribute
* @param attrType return type of attribute
* @param attr attribute info
* @return getter method for class
*/
private static String getGetterForClass(String attrName, String attrType) {
public static String getGetterForClass(AttributeInfo attr) {
String attrQuaifiedType = "";
if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
attrQuaifiedType = attr.getImportInfo().getPkgInfo() + ".";
}
attrQuaifiedType = attrQuaifiedType + attr.getImportInfo().getClassInfo();
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
if (!attr.isListAttr()) {
return getGetter(attrQuaifiedType, attributeName);
}
String listAttr = getListString() + attrQuaifiedType + UtilConstants.DIAMOND_CLOSE_BRACKET;
return getGetter(listAttr, attributeName);
}
/**
* Returns getter for attribute.
*
* @param type return type
* @param name attribute name
* @return getter for attribute
*/
private static String getGetter(String type, String name) {
return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
+ attrType + UtilConstants.SPACE + UtilConstants.GET_METHOD_PREFIX
+ JavaIdentifierSyntax.getCaptialCase(attrName) + UtilConstants.OPEN_PARENTHESIS
+ type + UtilConstants.SPACE + UtilConstants.GET_METHOD_PREFIX
+ JavaIdentifierSyntax.getCaptialCase(name) + UtilConstants.OPEN_PARENTHESIS
+ UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
+ UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.RETURN
+ UtilConstants.SPACE + attrName + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
+ UtilConstants.SPACE + name + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
}
/**
* Returns the setter method strings for class file.
*
* @param attrName name of the attribute
* @param attrType return type of attribute
* @param attr attribute info
* @param className name of the class
* @return setter method for class
*/
private static String getSetterForClass(String attrName, String attrType, String className) {
public static String getSetterForClass(AttributeInfo attr, String className) {
String attrQuaifiedType = "";
if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
attrQuaifiedType = attr.getImportInfo().getPkgInfo() + ".";
}
attrQuaifiedType = attrQuaifiedType + attr.getImportInfo().getClassInfo();
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
if (!attr.isListAttr()) {
return getSetter(className, attributeName, attrQuaifiedType);
}
String listAttr = getListString() + attrQuaifiedType + UtilConstants.DIAMOND_CLOSE_BRACKET;
return getSetter(className, attributeName, listAttr);
}
/**
* Returns setter for attribute.
*
* @param className class name
* @param name attribute name
* @param type return type
* @return setter for attribute
*/
private static String getSetter(String className, String name, String type) {
return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
+ className + UtilConstants.BUILDER + UtilConstants.SPACE + UtilConstants.SET_METHOD_PREFIX
+ JavaIdentifierSyntax.getCaptialCase(attrName) + UtilConstants.OPEN_PARENTHESIS
+ attrType + UtilConstants.SPACE + attrName + UtilConstants.CLOSE_PARENTHESIS
+ JavaIdentifierSyntax.getCaptialCase(name) + UtilConstants.OPEN_PARENTHESIS
+ type + UtilConstants.SPACE + name + UtilConstants.CLOSE_PARENTHESIS
+ UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE
+ UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.THIS + UtilConstants.PERIOD
+ attrName + UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.SPACE + attrName
+ UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION
+ name + UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.SPACE
+ name + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION
+ UtilConstants.RETURN + UtilConstants.SPACE + UtilConstants.THIS + UtilConstants.SEMI_COLAN
+ UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
}
/**
* Returns the setter method strings for class file.
*
* @param attr attribute info
* @return setter method for class
*/
public static String getSetterForTypeDefClass(AttributeInfo attr) {
String attrQuaifiedType = "";
if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
attrQuaifiedType = attr.getImportInfo().getPkgInfo() + ".";
}
attrQuaifiedType = attrQuaifiedType + attr.getImportInfo().getClassInfo();
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
if (!attr.isListAttr()) {
return getTypeDefSetter(attrQuaifiedType, attributeName);
}
String listAttr = getListString() + attrQuaifiedType + UtilConstants.DIAMOND_CLOSE_BRACKET;
return getTypeDefSetter(listAttr, attributeName);
}
/**
* Returns type def's setter for attribute.
*
* @param type data type
* @param name attribute name
* @return setter for type def's attribute
*/
private static String getTypeDefSetter(String type, String name) {
return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
+ UtilConstants.VOID + UtilConstants.SPACE + UtilConstants.SET_METHOD_PREFIX
+ JavaIdentifierSyntax.getCaptialCase(name) + UtilConstants.OPEN_PARENTHESIS
+ type + UtilConstants.SPACE + "value" + UtilConstants.CLOSE_PARENTHESIS
+ UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE
+ UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.THIS + UtilConstants.PERIOD
+ name + UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.SPACE
+ "value" + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.CLOSE_CURLY_BRACKET;
}
/**
* Returns override string.
*
* @return override string
*/
public static String getOverRideString() {
return UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.OVERRIDE + UtilConstants.NEW_LINE;
}
/**
* Returns the getter method strings for interface file.
*
* @param yangName name of the attribute
* @param returnType return type of attribute
* @param isList is list attribute
* @return getter method for interface
*/
private static String getGetterForInterface(String yangName, String returnType) {
public static String getGetterForInterface(String yangName, String returnType, boolean isList) {
if (!isList) {
return getGetterInterfaceString(returnType, yangName);
}
String listAttr = getListString() + returnType + UtilConstants.DIAMOND_CLOSE_BRACKET;
return getGetterInterfaceString(listAttr, yangName);
}
/**
* Returns getter for attribute in interface.
*
* @param returnType return type
* @param yangName attribute name
* @return getter for interface
*/
private static String getGetterInterfaceString(String returnType, String yangName) {
return UtilConstants.FOUR_SPACE_INDENTATION + returnType
+ UtilConstants.SPACE + UtilConstants.GET_METHOD_PREFIX
+ JavaIdentifierSyntax.getCaptialCase(yangName)
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS
+ UtilConstants.SEMI_COLAN;
}
/**
......@@ -232,9 +369,28 @@ public final class MethodsGenerator {
* @param attrName name of the attribute
* @param attrType return type of attribute
* @param className name of the java class being generated
* @param isList is list attribute
* @return setter method for interface
*/
private static String getSetterForInterface(String attrName, String attrType, String className) {
public static String getSetterForInterface(String attrName, String attrType, String className, boolean isList) {
if (!isList) {
return getSetterInterfaceString(className, attrName, attrType);
}
String listAttr = getListString() + attrType + UtilConstants.DIAMOND_CLOSE_BRACKET;
return getSetterInterfaceString(className, attrName, listAttr);
}
/**
* Returns setter string for interface.
*
* @param className class name
* @param attrName attribute name
* @param attrType attribute type
* @return setter string
*/
private static String getSetterInterfaceString(String className, String attrName, String attrType) {
return UtilConstants.FOUR_SPACE_INDENTATION + className + UtilConstants.BUILDER
+ UtilConstants.SPACE + UtilConstants.SET_METHOD_PREFIX
+ JavaIdentifierSyntax.getCaptialCase(attrName) + UtilConstants.OPEN_PARENTHESIS
......@@ -243,48 +399,47 @@ public final class MethodsGenerator {
}
/**
* Returns list string.
*
* @return list string
*/
private static String getListString() {
return UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET;
}
/**
* Returns the build method strings for interface file.
*
* @param yangName name of the interface
* @return build method for interface
*/
private static String getBuildForInterface(String yangName) {
public static String getBuildForInterface(String yangName) {
return UtilConstants.FOUR_SPACE_INDENTATION + yangName + UtilConstants.SPACE + UtilConstants.BUILD
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN;
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN
+ UtilConstants.NEW_LINE;
}
/**
* Returns the constructor strings for class file.
*
* @param yangName name of the class
* @param attr attribute info
* @return constructor for class
*/
private static String getConstructor(String yangName) {
String builderAttribute = yangName.substring(0, 1).toLowerCase() + yangName.substring(1);
String constructor = UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
+ yangName + UtilConstants.IMPL + UtilConstants.OPEN_PARENTHESIS + yangName + UtilConstants.BUILDER
+ UtilConstants.SPACE + builderAttribute + UtilConstants.OBJECT + UtilConstants.CLOSE_PARENTHESIS
+ UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE;
// TODO: need to get the partial constructor from constructor temp file.
// if (getAttrInfo() != null) {
// for (AttributeInfo attribute : getAttrInfo()) {
// attribute.setAttributeName(JavaIdentifierSyntax.getCamelCase(attribute.getAttributeName()));
// constructor = constructor + UtilConstants.TWELVE_SPACE_INDENTATION + UtilConstants.THIS
// + UtilConstants.PERIOD + attribute.getAttributeName() + UtilConstants.SPACE
// + UtilConstants.EQUAL + UtilConstants.SPACE + builderAttribute + UtilConstants.OBJECT
// + UtilConstants.PERIOD + UtilConstants.GET_METHOD_PREFIX
// + JavaIdentifierSyntax.getCaptialCase(attribute.getAttributeName())
// + UtilConstants.OPEN_PARENTHESIS
// + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN
// + UtilConstants.NEW_LINE;
// }
// getAttrInfo().clear();
// }
public static String getConstructor(String yangName, AttributeInfo attr) {
String builderAttribute = JavaIdentifierSyntax.getLowerCase(yangName);
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
String constructor = UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.THIS
+ UtilConstants.PERIOD + JavaIdentifierSyntax.getCamelCase(attributeName)
+ UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.SPACE + builderAttribute
+ UtilConstants.OBJECT + UtilConstants.PERIOD + UtilConstants.GET_METHOD_PREFIX
+ JavaIdentifierSyntax.getCaptialCase(JavaIdentifierSyntax.getCamelCase(attributeName))
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN
+ UtilConstants.NEW_LINE;
return constructor + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
return constructor;
}
/**
......@@ -293,10 +448,10 @@ public final class MethodsGenerator {
* @param yangName class name
* @return build method string for class
*/
private static String getBuild(String yangName) {
public static String getBuild(String yangName) {
return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE + yangName
+ UtilConstants.SPACE + UtilConstants.BUILD + UtilConstants.OPEN_PARENTHESIS
return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
+ yangName + UtilConstants.SPACE + UtilConstants.BUILD + UtilConstants.OPEN_PARENTHESIS
+ UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
+ UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.RETURN
+ UtilConstants.SPACE + UtilConstants.NEW + UtilConstants.SPACE + yangName + UtilConstants.IMPL
......@@ -309,14 +464,205 @@ public final class MethodsGenerator {
* Returns the Default constructor strings for class file.
*
* @param name name of the class
* @param modifierType modifier type for default constructor
* @return Default constructor for class
*/
private static String getDefaultConstructor(String name) {
private static String getDefaultConstructor(String name, String modifierType) {
return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE + name
return UtilConstants.FOUR_SPACE_INDENTATION + modifierType + UtilConstants.SPACE + name
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE
+ UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE;
}
/**
* Returns to string method open strings.
*
* @return to string method open string
*/
public static String getToStringMethodOpen() {
return getOverRideString() + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
+ UtilConstants.STRING + UtilConstants.SPACE + "to" + UtilConstants.STRING
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE
+ UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION
+ UtilConstants.RETURN + " MoreObjects.toStringHelper(getClass())" + UtilConstants.NEW_LINE;
}
/**
* Returns to string methods close string.
*
* @return to string method close string
*/
public static String getToStringMethodClose() {
return UtilConstants.TWELVE_SPACE_INDENTATION + ".to" + UtilConstants.STRING
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN
+ UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.CLOSE_CURLY_BRACKET;
}
/**
* To string method for class.
*
* @param attr attribute info
* @return to string method
*/
public static String getToStringMethod(AttributeInfo attr) {
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
return UtilConstants.TWELVE_SPACE_INDENTATION + UtilConstants.PERIOD + UtilConstants.ADD_STRING
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.QUOTES
+ attributeName + UtilConstants.QUOTES + UtilConstants.COMMA + UtilConstants.SPACE + attributeName
+ UtilConstants.CLOSE_PARENTHESIS;
}
/**
* Returns to hash code method open strings.
*
* @return to hash code method open string
*/
public static String getHashCodeMethodOpen() {
return getOverRideString() + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
+ UtilConstants.INT + UtilConstants.SPACE + UtilConstants.HASH_CODE_STRING
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE
+ UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION
+ UtilConstants.RETURN + " Objects.hash" + UtilConstants.OPEN_PARENTHESIS;
}
/**
* Returns to hash code methods close string.
*
* @param hashcodeString hash code string
* @return to hash code method close string
*/
public static String getHashCodeMethodClose(String hashcodeString) {
hashcodeString = YangIoUtils.trimAtLast(hashcodeString, UtilConstants.COMMA);
hashcodeString = YangIoUtils.trimAtLast(hashcodeString, UtilConstants.SPACE);
hashcodeString = YangIoUtils.partString(hashcodeString);
return hashcodeString + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
}
/**
* Hash code method for class.
*
* @param attr attribute info
* @return hash code method
*/
public static String getHashCodeMethod(AttributeInfo attr) {
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
return attributeName
+ UtilConstants.COMMA + UtilConstants.SPACE;
}
/**
* Returns to equals method open strings.
*
* @param className class name
* @return to equals method open string
*/
public static String getEqualsMethodOpen(String className) {
return getOverRideString() + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
+ UtilConstants.BOOLEAN + UtilConstants.SPACE + UtilConstants.EQUALS_STRING
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.OBJECT_STRING + UtilConstants.SPACE + "obj"
+ UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
+ UtilConstants.NEW_LINE + getEqualsMethodsCommonIfCondition()
+ getEqualsMethodsSpecificIfCondition(className);
}
/**
* Returns equal methods if condition string.
*
* @return if condition string
*/
private static String getEqualsMethodsCommonIfCondition() {
return UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.IF + UtilConstants.SPACE
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.THIS
+ UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.EQUAL + UtilConstants.SPACE + "obj"
+ UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
+ UtilConstants.NEW_LINE + UtilConstants.TWELVE_SPACE_INDENTATION + UtilConstants.RETURN
+ UtilConstants.SPACE + UtilConstants.TRUE + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
+ UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE;
}
/**
* Returns if condition for specific class object in equals method.
*
* @param className class name
* @return if condition string
*/
private static String getEqualsMethodsSpecificIfCondition(String className) {
return UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.IF + UtilConstants.SPACE
+ UtilConstants.OPEN_PARENTHESIS + "obj" + UtilConstants.INSTANCE_OF + className
+ UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
+ UtilConstants.NEW_LINE + UtilConstants.TWELVE_SPACE_INDENTATION + className + UtilConstants.SPACE
+ "other " + UtilConstants.EQUAL + UtilConstants.SPACE + UtilConstants.OPEN_PARENTHESIS + className
+ UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + "obj" + UtilConstants.SEMI_COLAN
+ UtilConstants.NEW_LINE + UtilConstants.TWELVE_SPACE_INDENTATION + UtilConstants.RETURN
+ UtilConstants.NEW_LINE;
}
/**
* Returns to equals methods close string.
*
* @param equalMethodString equal method string
* @return to equals method close string
*/
public static String getEqualsMethodClose(String equalMethodString) {
equalMethodString = YangIoUtils.trimAtLast(equalMethodString, UtilConstants.AND);
equalMethodString = YangIoUtils.trimAtLast(equalMethodString, UtilConstants.AND);
equalMethodString = YangIoUtils.trimAtLast(equalMethodString, UtilConstants.SPACE);
equalMethodString = YangIoUtils.trimAtLast(equalMethodString, UtilConstants.NEW_LINE) + UtilConstants.SEMI_COLAN
+ UtilConstants.NEW_LINE;
return equalMethodString + UtilConstants.EIGHT_SPACE_INDENTATION
+ UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE
+ UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.RETURN + UtilConstants.SPACE
+ UtilConstants.FALSE + UtilConstants.SEMI_COLAN
+ UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.CLOSE_CURLY_BRACKET;
}
/**
* Equals method for class.
*
* @param attr attribute info
* @return equals method
*/
public static String getEqualsMethod(AttributeInfo attr) {
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
return UtilConstants.SIXTEEN_SPACE_INDENTATION + UtilConstants.SPACE + UtilConstants.OBJECT_STRING + "s"
+ UtilConstants.PERIOD + UtilConstants.EQUALS_STRING + UtilConstants.OPEN_PARENTHESIS + attributeName
+ UtilConstants.COMMA + UtilConstants.SPACE + "other." + attributeName + UtilConstants.CLOSE_PARENTHESIS
+ UtilConstants.SPACE + UtilConstants.AND + UtilConstants.AND;
}
/**
* Returns of method string for class.
*
* @param name class name
* @param attr attribute info
* @return of method string
*/
public static String getOfMethod(String name, AttributeInfo attr) {
String attrQuaifiedType = "";
if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
attrQuaifiedType = attr.getImportInfo().getPkgInfo() + ".";
}
attrQuaifiedType = attrQuaifiedType + attr.getImportInfo().getClassInfo();
return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.STATIC
+ UtilConstants.SPACE + name + UtilConstants.SPACE + UtilConstants.OF + UtilConstants.OPEN_PARENTHESIS
+ attrQuaifiedType + UtilConstants.SPACE + UtilConstants.VALUE + UtilConstants.CLOSE_PARENTHESIS
+ UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE
+ UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.RETURN + UtilConstants.SPACE + UtilConstants.NEW
+ UtilConstants.SPACE + name + UtilConstants.OPEN_PARENTHESIS + UtilConstants.VALUE
+ UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE;
}
}
......
/*
* 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;
/**
* Data Store types.
*/
public enum TempDataStoreTypes {
/**
* Getter methods for interfaces.
*/
GETTER_METHODS,
/**
* Getter methods impl for classes.
*/
GETTER_METHODS_IMPL,
/**
* Setter methods for interfaces.
*/
SETTER_METHODS,
/**
* Setter methods impl for classes.
*/
SETTER_METHODS_IMPL,
/**
* Constructor for impl class.
*/
CONSTRUCTOR,
/**
* Attributes.
*/
ATTRIBUTE,
/**
* TypeDef.
*/
TYPE_DEF,
/**
* ToString method.
*/
TO_STRING,
/**
* HashCode method.
*/
HASH_CODE,
/**
* Equals method.
*/
EQUALS
}
......@@ -256,6 +256,7 @@ public final class UtilConstants {
public static final String BUILD = "build";
public static final String OBJECT = "Object";
public static final String OVERRIDE = "@Override";
public static final String CHILDREN = "'s children";
/**
* For collections.
......
......@@ -104,7 +104,7 @@ public final class FileSystemUtil {
*/
public static void appendFileContents(File toAppend, File srcFile) throws IOException {
insertStringInFile(srcFile, UtilConstants.NEW_LINE + readAppendFile(toAppend.toString()));
updateFileHandle(srcFile, UtilConstants.NEW_LINE + readAppendFile(toAppend.toString()), false);
return;
}
......@@ -133,18 +133,23 @@ public final class FileSystemUtil {
}
/**
* Insert content to the generated file.
* Update the generated file handle.
*
* @param inputFile input file
* @param contentTobeAdded content to be appended to the file
* @param isClose when close of file is called.
* @throws IOException when fails to append content to the file
*/
public static void insertStringInFile(File inputFile, String contentTobeAdded) throws IOException {
public static void updateFileHandle(File inputFile, String contentTobeAdded, boolean isClose) throws IOException {
FileWriter fileWriter = new FileWriter(inputFile, true);
PrintWriter outputPrintWriter = new PrintWriter(fileWriter);
outputPrintWriter.write(contentTobeAdded);
outputPrintWriter.flush();
outputPrintWriter.close();
if (!isClose) {
outputPrintWriter.write(contentTobeAdded);
outputPrintWriter.flush();
outputPrintWriter.close();
} else {
fileWriter.flush();
fileWriter.close();
}
}
}
......
......@@ -71,6 +71,21 @@ public final class JavaDocGen {
SETTER,
/**
* For type def's setters.
*/
TYPE_DEF_SETTER,
/**
* For type def's constructor.
*/
TYPE_DEF_CONSTRUCTOR,
/**
* For of method.
*/
OF,
/**
* For default constructor.
*/
DEFAULT_CONSTRUCTOR,
......@@ -91,10 +106,11 @@ public final class JavaDocGen {
*
* @param type java doc type
* @param name name of the YangNode
* @return javadocs
* @param isList is list attribute
* @return javadocs.
*/
public static String getJavaDoc(JavaDocType type, String name) {
name = JavaIdentifierSyntax.getCamelCase(name);
public static String getJavaDoc(JavaDocType type, String name, boolean isList) {
name = JavaIdentifierSyntax.getLowerCase(JavaIdentifierSyntax.getCamelCase(name));
String javaDoc = "";
if (type.equals(JavaDocType.IMPL_CLASS)) {
javaDoc = generateForImplClass(name);
......@@ -107,9 +123,15 @@ public final class JavaDocGen {
} else if (type.equals(JavaDocType.PACKAGE_INFO)) {
javaDoc = generateForPackage(name);
} else if (type.equals(JavaDocType.GETTER)) {
javaDoc = generateForGetters(name);
javaDoc = generateForGetters(name, isList);
} else if (type.equals(JavaDocType.TYPE_DEF_SETTER)) {
javaDoc = generateForTypeDefSetter(name);
} else if (type.equals(JavaDocType.TYPE_DEF_CONSTRUCTOR)) {
javaDoc = generateForTypeDefConstructor(name);
} else if (type.equals(JavaDocType.SETTER)) {
javaDoc = generateForSetters(name);
javaDoc = generateForSetters(name, isList);
} else if (type.equals(JavaDocType.OF)) {
javaDoc = generateForOf(name);
} else if (type.equals(JavaDocType.DEFAULT_CONSTRUCTOR)) {
javaDoc = generateForDefaultConstructors();
} else if (type.equals(JavaDocType.BUILD)) {
......@@ -124,32 +146,96 @@ public final class JavaDocGen {
* Generate javaDocs for getter method.
*
* @param attribute attribute
* @param isList is list attribute
* @return javaDocs
*/
private static String generateForGetters(String attribute) {
return UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE
private static String generateForGetters(String attribute, boolean isList) {
String getter = UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.JAVA_DOC_FIRST_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_GETTERS + attribute
+ UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.NEW_LINE_ESTRIC + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_RETURN
+ attribute + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.NEW_LINE_ESTRIC + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_RETURN;
if (isList) {
attribute = UtilConstants.LIST.toLowerCase() + UtilConstants.SPACE + UtilConstants.OF + UtilConstants.SPACE
+ attribute;
}
getter = getter + attribute + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.JAVA_DOC_END_LINE;
return getter;
}
/**
* Generates javaDocs for setter method.
*
* @param attribute attribute
* @param isList is list attribute
* @return javaDocs
*/
private static String generateForSetters(String attribute) {
return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE
private static String generateForSetters(String attribute, boolean isList) {
String setter = UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.JAVA_DOC_FIRST_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_SETTERS + attribute
+ UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.NEW_LINE_ESTRIC + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_PARAM
+ attribute + UtilConstants.SPACE + attribute + UtilConstants.NEW_LINE
+ attribute + UtilConstants.SPACE;
if (isList) {
attribute = UtilConstants.LIST.toLowerCase() + UtilConstants.SPACE + UtilConstants.OF + UtilConstants.SPACE
+ attribute;
}
setter = setter + attribute + UtilConstants.NEW_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_RETURN + UtilConstants.BUILDER_OBJECT
+ attribute + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.JAVA_DOC_END_LINE;
return setter;
}
/**
* Generates javaDocs for of method.
*
* @param attribute attribute
* @return javaDocs
*/
private static String generateForOf(String attribute) {
return UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_OF + attribute
+ UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.NEW_LINE_ESTRIC + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_PARAM
+ UtilConstants.VALUE + UtilConstants.SPACE + UtilConstants.VALUE + UtilConstants.NEW_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_RETURN + UtilConstants.OBJECT
+ UtilConstants.SPACE + UtilConstants.OF + UtilConstants.SPACE + attribute + UtilConstants.NEW_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_END_LINE;
}
/**
* Generates javaDocs for typedef setter method.
*
* @param attribute attribute
* @return javaDocs
*/
private static String generateForTypeDefSetter(String attribute) {
return (UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_SETTERS_COMMON + attribute
+ UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.NEW_LINE_ESTRIC + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_PARAM
+ UtilConstants.VALUE + UtilConstants.SPACE + UtilConstants.VALUE + UtilConstants.NEW_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_END_LINE);
}
/**
* Generates javaDocs for typedef constructor.
*
* @param attribute attribute
* @return javaDocs
*/
private static String generateForTypeDefConstructor(String attribute) {
return (UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_CONSTRUCTOR + attribute
+ UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.NEW_LINE_ESTRIC + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_PARAM
+ UtilConstants.VALUE + UtilConstants.SPACE + UtilConstants.VALUE + UtilConstants.NEW_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_END_LINE);
}
/**
......@@ -170,8 +256,8 @@ public final class JavaDocGen {
* @return javaDocs
*/
private static String generateForBuilderClass(String className) {
return UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.BUILDER_CLASS_JAVA_DOC + className
+ UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE;
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;
}
/**
......@@ -181,8 +267,8 @@ public final class JavaDocGen {
* @return javaDocs
*/
private static String generateForInterface(String interfaceName) {
return UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.INTERFACE_JAVA_DOC + interfaceName
+ UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE;
return UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.INTERFACE_JAVA_DOC
+ interfaceName + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE;
}
/**
......
......@@ -38,7 +38,7 @@ public final class YangFileScanner {
*
* @param root specified directory
* @return list of java files
* @throws NullPointerException when no files are there
* @throws NullPointerException when no files are there.
* @throws IOException when files get deleted while performing the
* operations
*/
......@@ -67,9 +67,8 @@ public final class YangFileScanner {
* @return list of required files
* @throws IOException when files get deleted while performing the
* operations
* @throws NullPointerException null pointer access
*/
public static List<String> getFiles(String root, String extension) throws NullPointerException, IOException {
public static List<String> getFiles(String root, String extension) throws NullPointerException, IOException {
List<String> store = new LinkedList<>();
Stack<String> stack = new Stack<>();
stack.push(root);
......
......@@ -16,6 +16,8 @@
package org.onosproject.yangutils.utils.io.impl;
import static org.slf4j.LoggerFactory.getLogger;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
......@@ -29,8 +31,6 @@ import org.onosproject.yangutils.utils.UtilConstants;
import org.slf4j.Logger;
import org.sonatype.plexus.build.incremental.BuildContext;
import static org.slf4j.LoggerFactory.getLogger;
/**
* Provides common utility functionalities for code generation.
*/
......@@ -80,7 +80,7 @@ public final class YangIoUtils {
fileWriter = new FileWriter(packageInfo);
bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(CopyrightHeader.getCopyrightHeader());
bufferedWriter.write(JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.PACKAGE_INFO, classInfo));
bufferedWriter.write(JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.PACKAGE_INFO, classInfo, false));
bufferedWriter.write(UtilConstants.PACKAGE + UtilConstants.SPACE + pack + UtilConstants.SEMI_COLAN);
bufferedWriter.close();
} catch (IOException e) {
......@@ -127,4 +127,38 @@ public final class YangIoUtils {
log.info("Source directory added to compilation root: " + source);
}
/**
* Removes extra char from the string.
*
* @param valueString string to be trimmed
* @param removealStirng extra chars
* @return new string
*/
public static String trimAtLast(String valueString, String removealStirng) {
StringBuilder stringBuilder = new StringBuilder(valueString);
int index = valueString.lastIndexOf(removealStirng);
stringBuilder.deleteCharAt(index);
return stringBuilder.toString();
}
/**
* Returns new parted string.
*
* @param partString string to be parted
* @return parted string
*/
public static String partString(String partString) {
String[] strArray = partString.split(UtilConstants.COMMA);
String newString = "";
for (int i = 0; i < strArray.length; i++) {
if (i % 4 != 0) {
newString = newString + strArray[i] + UtilConstants.COMMA;
} else {
newString = newString + UtilConstants.NEW_LINE + UtilConstants.TWELVE_SPACE_INDENTATION + strArray[i]
+ UtilConstants.COMMA;
}
}
return trimAtLast(newString, UtilConstants.COMMA);
}
}
......
......@@ -20,6 +20,10 @@ 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;
......@@ -33,7 +37,7 @@ import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
*/
public class CachedJavaFileHandleTest {
private static final String DIR_PKG = "target/unit/cachedfile/";
private static final String DIR_PKG = "target/unit/cachedfile/yangmodel/";
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";
......@@ -42,7 +46,7 @@ public class CachedJavaFileHandleTest {
/**
* Unit test case for add attribute info.
*
* @throws IOException when fails to add an attribute.
* @throws IOException when fails to add an attribute
*/
@Test
public void testForAddAttributeInfo() throws IOException {
......@@ -55,39 +59,23 @@ public class CachedJavaFileHandleTest {
/**
* Unit test case for close of cached files.
*
* @throws IOException when fails to generate files.
* @throws IOException when fails to generate files
*/
@Test
public void testForClose() throws IOException {
// TODO: update to new framework.
// 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 {
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()));
}
/**
......@@ -113,14 +101,14 @@ public class CachedJavaFileHandleTest {
/**
* Returns cached java file handle.
*
* @return 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.setRelativeFilePath(DIR_PKG + PKG.replace(".", "/"));
fileHandle.setRelativeFilePath(PKG.replace(".", "/"));
fileHandle.setCodeGenFilePath(DIR_PKG);
return fileHandle;
}
......
......@@ -16,17 +16,16 @@
package org.onosproject.yangutils.translator.tojava.utils;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.junit.Test;
import org.onosproject.yangutils.translator.GeneratedFileType;
import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes;
import org.onosproject.yangutils.translator.tojava.TraversalType;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.hamcrest.core.Is.is;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import static org.junit.Assert.assertNotNull;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
/**
......@@ -40,18 +39,14 @@ public final class ClassDefinitionGeneratorTest {
* @throws SecurityException if any security violation is observed
* @throws NoSuchMethodException if when the method is not found
* @throws IllegalArgumentException if there is illegal argument found
* @throws InstantiationException if instantiation is provoked for the
* private constructor
* @throws IllegalAccessException if instance is provoked or a method is
* provoked
* @throws InvocationTargetException when an exception occurs by the method
* or constructor
* @throws InstantiationException if instantiation is provoked for the private constructor
* @throws IllegalAccessException if instance is provoked or a method is provoked
* @throws InvocationTargetException when an exception occurs by the method or constructor
*/
@Test
public void callPrivateConstructors() throws SecurityException, NoSuchMethodException,
IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
Class<?>[] classesToConstruct = {
ClassDefinitionGenerator.class };
public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
InstantiationException, IllegalAccessException, InvocationTargetException {
Class<?>[] classesToConstruct = {ClassDefinitionGenerator.class };
for (Class<?> clazz : classesToConstruct) {
Constructor<?> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
......@@ -105,14 +100,14 @@ public final class ClassDefinitionGeneratorTest {
}
/**
* Unit test for invalid generated type.
* Unit test for typedef generated type.
*/
@Test
public void generateInvalidDefinitionTest() {
public void generateTypeDefTest() {
// String invalidDefinition = ClassDefinitionGenerator
// .generateClassDefinition(GeneratedFileType.GENERATE_INTERFACE_WITH_BUILDER, "invalid");
// assertThat(true, is(invalidDefinition == null));
String typeDef = ClassDefinitionGenerator.generateClassDefinition(GeneratedFileType.GENERATE_TYPEDEF_CLASS,
"invalid");
assertThat(true, is(typeDef.contains(UtilConstants.CLASS)));
}
/**
......@@ -123,5 +118,6 @@ public final class ClassDefinitionGeneratorTest {
TraversalType.valueOf(TraversalType.CHILD.toString());
GeneratedMethodTypes.valueOf(GeneratedMethodTypes.CONSTRUCTOR.toString());
TempDataStoreTypes.valueOf(TempDataStoreTypes.CONSTRUCTOR.toString());
}
}
......
......@@ -25,8 +25,12 @@ import org.onosproject.yangutils.translator.tojava.ImportInfo;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.hamcrest.core.Is.is;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/**
* Unit test cases for java code snippet generator.
*/
......@@ -40,6 +44,27 @@ public class JavaCodeSnippetGenTest {
private static final String STRING = "String";
/**
* Unit test for private constructor.
*
* @throws SecurityException if any security violation is observed
* @throws NoSuchMethodException if when the method is not found
* @throws IllegalArgumentException if there is illegal argument found
* @throws InstantiationException if instantiation is provoked for the private constructor
* @throws IllegalAccessException if instance is provoked or a method is provoked
* @throws InvocationTargetException when an exception occurs by the method or constructor
*/
@Test
public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
InstantiationException, IllegalAccessException, InvocationTargetException {
Class<?>[] classesToConstruct = {JavaCodeSnippetGen.class };
for (Class<?> clazz : classesToConstruct) {
Constructor<?> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
assertNotNull(constructor.newInstance());
}
}
/**
* Unit test case for import text.
*/
@Test
......@@ -64,27 +89,6 @@ public class JavaCodeSnippetGenTest {
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() {
// TODO: need to update for new framework
// String attributeWithType
// = JavaCodeSnippetGen.getJavaAttributeDefination(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.getJavaAttributeDefination(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)));
}
......@@ -99,29 +103,62 @@ public class JavaCodeSnippetGenTest {
}
/**
* Unit test case for java method info.
* Unit test case for java class interface definition close.
*/
@Test
public void testForJavaMethodInfo() {
//TODO: update to new framework.
// 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)));
public void testForJavaClassDefInterfaceClose() {
String interfaceDef = JavaCodeSnippetGen.getJavaClassDefClose(FILE_GEN_TYPE, YANG_NAME);
assertThat(true, is(interfaceDef.equals(UtilConstants.CLOSE_CURLY_BRACKET)));
}
/**
* Unit test case for java class definition close.
* Unit test case for java class builder 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)));
public void testForJavaClassDefBuilderClassClose() {
String builderClassDef = JavaCodeSnippetGen.getJavaClassDefClose(GeneratedFileType.BUILDER_CLASS_MASK,
YANG_NAME);
assertThat(true, is(builderClassDef.equals(UtilConstants.CLOSE_CURLY_BRACKET)));
}
/**
* Unit test case for java class typedef definition close.
*/
@Test
public void testForJavaClassDefTypeDefClose() {
String typeDef = JavaCodeSnippetGen.getJavaClassDefClose(GeneratedFileType.GENERATE_TYPEDEF_CLASS, YANG_NAME);
assertThat(true, is(typeDef.equals(UtilConstants.CLOSE_CURLY_BRACKET)));
}
/**
* Unit test case for java attribute info.
*/
@SuppressWarnings("rawtypes")
@Test
public void testForJavaAttributeInfo() {
String attributeWithoutTypePkg = JavaCodeSnippetGen.getJavaAttributeDefination(null, "String", YANG_NAME,
false);
assertThat(true, is(attributeWithoutTypePkg.equals(UtilConstants.PRIVATE + UtilConstants.SPACE + "String"
+ UtilConstants.SPACE + YANG_NAME + UtilConstants.SEMI_COLAN)));
String attributeWithTypePkg = JavaCodeSnippetGen.getJavaAttributeDefination("java.lang", "String", YANG_NAME,
false);
assertThat(true, is(attributeWithTypePkg.equals(UtilConstants.PRIVATE + UtilConstants.SPACE + "java.lang."
+ "String" + UtilConstants.SPACE + YANG_NAME + UtilConstants.SEMI_COLAN)));
String attributeWithListPkg = JavaCodeSnippetGen.getJavaAttributeDefination("java.lang", "String", YANG_NAME,
true);
assertThat(true,
is(attributeWithListPkg.equals(UtilConstants.PRIVATE + UtilConstants.SPACE + UtilConstants.LIST
+ UtilConstants.DIAMOND_OPEN_BRACKET + "java.lang."
+ "String" + UtilConstants.DIAMOND_CLOSE_BRACKET + UtilConstants.SPACE + YANG_NAME
+ UtilConstants.SEMI_COLAN)));
String attributeWithListWithoutPkg = JavaCodeSnippetGen.getJavaAttributeDefination(null, "String", YANG_NAME,
true);
assertThat(true,
is(attributeWithListWithoutPkg.equals(UtilConstants.PRIVATE + UtilConstants.SPACE + UtilConstants.LIST
+ UtilConstants.DIAMOND_OPEN_BRACKET + "String"
+ UtilConstants.DIAMOND_CLOSE_BRACKET + UtilConstants.SPACE + YANG_NAME
+ UtilConstants.SEMI_COLAN)));
}
/**
......
......@@ -36,8 +36,8 @@ public final class JavaIdentifierSyntaxTest {
public static final String DATE2 = "1992-01-25";
public static final String PARENT_WITH_PERIOD = "test5.test6.test7";
public static final String CHILD_WITH_PERIOD = "test1.test2.test3";
public static final String DATE_WITH_REV1 = "rev000105";
public static final String DATE_WITH_REV2 = "rev920125";
public static final String DATE_WITH_REV1 = "rev20000105";
public static final String DATE_WITH_REV2 = "rev19920125";
public static final String VERSION_NUMBER = "v1";
public static final String INVALID_NAME_SPACE1 = "byte:#test2:9test3";
public static final String INVALID_NAME_SPACE2 = "const:#test2://9test3";
......
......@@ -16,17 +16,17 @@
package org.onosproject.yangutils.translator.tojava.utils;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.junit.Test;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.translator.GeneratedFileType;
import org.onosproject.yangutils.translator.tojava.AttributeInfo;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertNotNull;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.translator.tojava.AttributeInfo;
import org.onosproject.yangutils.translator.tojava.ImportInfo;
import org.onosproject.yangutils.utils.UtilConstants;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/**
* Unit tests for generated methods from the file type.
......@@ -42,19 +42,15 @@ public final class MethodsGeneratorTest {
* @throws SecurityException if any security violation is observed
* @throws NoSuchMethodException if when the method is not found
* @throws IllegalArgumentException if there is illegal argument found
* @throws InstantiationException if instantiation is provoked for the
* private constructor
* @throws IllegalAccessException if instance is provoked or a method is
* provoked
* @throws InvocationTargetException when an exception occurs by the method
* or constructor
* @throws InstantiationException if instantiation is provoked for the private constructor
* @throws IllegalAccessException if instance is provoked or a method is provoked
* @throws InvocationTargetException when an exception occurs by the method or constructor
*/
@Test
public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
InstantiationException, IllegalAccessException, InvocationTargetException {
InstantiationException, IllegalAccessException, InvocationTargetException {
Class<?>[] classesToConstruct = {
MethodsGenerator.class };
Class<?>[] classesToConstruct = {MethodsGenerator.class };
for (Class<?> clazz : classesToConstruct) {
Constructor<?> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
......@@ -63,27 +59,100 @@ public final class MethodsGeneratorTest {
}
/**
* Unit test for checking the values received from constructor, default
* constructor and build string formation.
* Unit test case for checking the parse builder and typedef constructor.
*/
@Test
public void getParseBuilderInterfaceMethodConstructorTest() {
ImportInfo forSetter = new ImportInfo();
attrType.setDataTypeName("binary");
attrType.getDataTypeName();
attrType.setDataType(YangDataTypes.BINARY);
attrType.getDataType();
testAttr.setAttributeName("attributeTest");
testAttr.setAttributeType(attrType);
forSetter.setPkgInfo("test1/test3");
forSetter.setClassInfo("This class contains");
testAttr.setImportInfo(forSetter);
String parseBuilderInterface = MethodsGenerator.parseBuilderInterfaceMethodString(testAttr, "newTestName");
assertThat(parseBuilderInterface.contains("attributeTest") && parseBuilderInterface.contains("newTestName"),
is(true));
String parseBuilderInterfaceBuild = MethodsGenerator.parseBuilderInterfaceBuildMethodString("testname7");
assertThat(parseBuilderInterfaceBuild.contains("Builds object of")
&& parseBuilderInterfaceBuild.contains("testname7"), is(true));
String stringTypeDef = MethodsGenerator.getTypeDefConstructor(testAttr, "Testname");
}
/**
* Unit test case for checking the values received from constructor, default constructor and build string formation.
*/
@Test
public void getValuesTest() {
String stringConstructor = MethodsGenerator.getConstructorString("testname");
assertThat(
stringConstructor.contains("Construct the object of testnameImpl.")
&& stringConstructor.contains("@param testnameObject builder object of testname")
&& stringConstructor.contains("public testnameImpl(testnameBuilder testnameObject) {"),
is(true));
String stringDefaultConstructor = MethodsGenerator.getDefaultConstructorString(
GeneratedFileType.BUILDER_CLASS_MASK,
"testname");
assertThat(stringDefaultConstructor.contains("Default Constructor.")
&& stringDefaultConstructor.contains("public testnameBuilder() {")
&& stringDefaultConstructor.contains("}"), is(true));
assertThat(stringConstructor.contains(UtilConstants.JAVA_DOC_CONSTRUCTOR)
&& stringConstructor.contains(UtilConstants.JAVA_DOC_PARAM)
&& stringConstructor.contains(UtilConstants.BUILDER_OBJECT), is(true));
String stringDefaultConstructor = MethodsGenerator.getDefaultConstructorString("testnameBuilder", "public");
assertThat(stringDefaultConstructor.contains(UtilConstants.JAVA_DOC_DEFAULT_CONSTRUCTOR)
&& stringDefaultConstructor.contains(UtilConstants.BUILDER)
&& stringDefaultConstructor.contains("testname"), is(true));
String stringBuild = MethodsGenerator.getBuildString("testname");
assertThat(
stringBuild.contains("public testname build() {")
&& stringBuild.contains("return new testnameImpl(this);") && stringBuild.contains("}"),
assertThat(stringBuild.contains(UtilConstants.OVERRIDE) && stringBuild.contains(UtilConstants.BUILD)
&& stringBuild.contains(UtilConstants.RETURN), is(true));
}
/**
* Unit test for checking the values received for class getter, class and typedef setters with list data type.
*/
@Test
public void getGetterSetterTest() {
ImportInfo forGetterSetter = new ImportInfo();
attrType.setDataTypeName("int");
attrType.getDataTypeName();
attrType.setDataType(YangDataTypes.UINT8);
attrType.getDataType();
testAttr.setAttributeName("AttributeTest1");
testAttr.setAttributeType(attrType);
forGetterSetter.setPkgInfo(null);
forGetterSetter.setClassInfo("This class contains");
testAttr.setImportInfo(forGetterSetter);
testAttr.setListAttr(true);
String getterForClass = MethodsGenerator.getGetterForClass(testAttr);
assertThat(getterForClass.contains(UtilConstants.GET_METHOD_PREFIX) && getterForClass.contains("List<")
&& getterForClass.contains("attributeTest1"), is(true));
String setterForClass = MethodsGenerator.getSetterForClass(testAttr, "TestThis");
assertThat(setterForClass.contains(UtilConstants.SET_METHOD_PREFIX) && setterForClass.contains("List<")
&& setterForClass.contains("attributeTest1"), is(true));
String typeDefSetter = MethodsGenerator.getSetterForTypeDefClass(testAttr);
assertThat(typeDefSetter.contains(UtilConstants.SET_METHOD_PREFIX) && typeDefSetter.contains("List<")
&& typeDefSetter.contains("attributeTest1") && typeDefSetter.contains("this."), is(true));
}
/**
* Unit test case for checking the parse builder and typedef constructor with list data type.
*/
@Test
public void getConstructorWithListTypeTest() {
ImportInfo forSetter = new ImportInfo();
attrType.setDataTypeName("binary");
attrType.getDataTypeName();
attrType.setDataType(YangDataTypes.BINARY);
attrType.getDataType();
testAttr.setAttributeName("attributeTest");
testAttr.setAttributeType(attrType);
forSetter.setPkgInfo(null);
forSetter.setClassInfo("This class contains");
testAttr.setImportInfo(forSetter);
testAttr.setListAttr(true);
String parseBuilderInterface = MethodsGenerator.parseBuilderInterfaceMethodString(testAttr, "newTestName");
assertThat(parseBuilderInterface.contains("attributeTest") && parseBuilderInterface.contains("List<"),
is(true));
String parseBuilderInterfaceBuild = MethodsGenerator.parseBuilderInterfaceBuildMethodString("testname7");
assertThat(parseBuilderInterfaceBuild.contains("Builds object of")
&& parseBuilderInterfaceBuild.contains("testname7"), is(true));
String stringTypeDef = MethodsGenerator.getTypeDefConstructor(testAttr, "Testname");
assertThat(stringTypeDef.contains("(List<") && stringTypeDef.contains("Testname")
&& stringTypeDef.contains(UtilConstants.THIS), is(true));
}
}
\ No newline at end of file
}
......
......@@ -16,22 +16,25 @@
package org.onosproject.yangutils.utils.io.impl;
import static org.slf4j.LoggerFactory.getLogger;
import org.junit.Test;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import org.onosproject.yangutils.translator.GeneratedFileType;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.onosproject.yangutils.translator.GeneratedFileType;
import org.onosproject.yangutils.utils.UtilConstants;
import org.slf4j.Logger;
import static org.slf4j.LoggerFactory.getLogger;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
/**
* Tests the file handle utilities.
......@@ -50,12 +53,12 @@ public final class FileSystemUtilTest {
/**
* A private constructor is tested.
*
* @throws SecurityException if any security violation is observed.
* @throws NoSuchMethodException if when the method is not found.
* @throws IllegalArgumentException if there is illegal argument found.
* @throws InstantiationException if instantiation is provoked for the private constructor.
* @throws IllegalAccessException if instance is provoked or a method is provoked.
* @throws InvocationTargetException when an exception occurs by the method or constructor.
* @throws SecurityException if any security violation is observed
* @throws NoSuchMethodException if when the method is not found
* @throws IllegalArgumentException if there is illegal argument found
* @throws InstantiationException if instantiation is provoked for the private constructor
* @throws IllegalAccessException if instance is provoked or a method is provoked
* @throws InvocationTargetException when an exception occurs by the method or constructor
*/
@Test
public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
......@@ -82,17 +85,18 @@ public final class FileSystemUtilTest {
* This test case checks the contents to be written in the file.
*/
@Test
public void insertStringInFileTest() throws IOException {
public void updateFileHandleTest() throws IOException {
File dir = new File(baseDir + File.separator + "File1");
dir.mkdirs();
File createFile = new File(dir + "testFile");
createFile.createNewFile();
File createSourceFile = new File(dir + "sourceTestFile");
createSourceFile.createNewFile();
FileSystemUtil.insertStringInFile(createFile, "This is to append a text to the file first1\n");
FileSystemUtil.insertStringInFile(createFile, "This is next second line\n");
FileSystemUtil.insertStringInFile(createFile, "This is next third line in the file");
FileSystemUtil.updateFileHandle(createFile, "This is to append a text to the file first1\n", false);
FileSystemUtil.updateFileHandle(createFile, "This is next second line\n", false);
FileSystemUtil.updateFileHandle(createFile, "This is next third line in the file", false);
FileSystemUtil.appendFileContents(createFile, createSourceFile);
FileSystemUtil.updateFileHandle(createFile, null, true);
}
/**
......
......@@ -41,7 +41,7 @@ public final class JavaDocGenTest {
@Test
public void builderClassGenerationTest() {
String builderClassJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.BUILDER_CLASS, "testGeneration1");
String builderClassJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.BUILDER_CLASS, "testGeneration1", false);
assertTrue(builderClassJavaDoc.contains("Provides the builder implementation of")
&& builderClassJavaDoc.contains(" */\n"));
}
......@@ -52,7 +52,7 @@ public final class JavaDocGenTest {
@Test
public void builderInterfaceGenerationTest() {
String builderInterfaceJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.BUILDER_INTERFACE, "testGeneration1");
String builderInterfaceJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.BUILDER_INTERFACE, "testGeneration1", false);
assertTrue(builderInterfaceJavaDoc.contains("Builder for") && builderInterfaceJavaDoc.contains(" */\n"));
}
......@@ -62,19 +62,19 @@ public final class JavaDocGenTest {
@Test
public void buildGenerationTest() {
String buildDoc = JavaDocGen.getJavaDoc(JavaDocType.BUILD, "testGeneration1");
String buildDoc = JavaDocGen.getJavaDoc(JavaDocType.BUILD, "testGeneration1", false);
assertTrue(buildDoc.contains("Builds object of") && buildDoc.contains(" */\n"));
}
/**
* A private constructor is tested.
*
* @throws SecurityException if any security violation is observed.
* @throws NoSuchMethodException if when the method is not found.
* @throws IllegalArgumentException if there is illegal argument found.
* @throws InstantiationException if instantiation is provoked for the private constructor.
* @throws IllegalAccessException if instance is provoked or a method is provoked.
* @throws InvocationTargetException when an exception occurs by the method or constructor.
* @throws SecurityException if any security violation is observed
* @throws NoSuchMethodException if when the method is not found
* @throws IllegalArgumentException if there is illegal argument found
* @throws InstantiationException if instantiation is provoked for the private constructor
* @throws IllegalAccessException if instance is provoked or a method is provoked
* @throws InvocationTargetException when an exception occurs by the method or constructor
*/
@Test
public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
......@@ -94,7 +94,7 @@ public final class JavaDocGenTest {
@Test
public void constructorGenerationTest() {
String constructorDoc = JavaDocGen.getJavaDoc(JavaDocType.CONSTRUCTOR, "testGeneration1");
String constructorDoc = JavaDocGen.getJavaDoc(JavaDocType.CONSTRUCTOR, "testGeneration1", false);
assertTrue(
constructorDoc.contains("Construct the object of") && constructorDoc.contains("builder object of")
&& constructorDoc.contains("@param") && constructorDoc.contains("*/\n"));
......@@ -107,7 +107,7 @@ public final class JavaDocGenTest {
@Test
public void defaultConstructorGenerationTest() {
String defaultConstructorDoc = JavaDocGen.getJavaDoc(JavaDocType.DEFAULT_CONSTRUCTOR, "testGeneration1");
String defaultConstructorDoc = JavaDocGen.getJavaDoc(JavaDocType.DEFAULT_CONSTRUCTOR, "testGeneration1", false);
assertTrue(defaultConstructorDoc.contains("Default Constructor") && defaultConstructorDoc.contains(" */\n"));
}
......@@ -117,7 +117,7 @@ public final class JavaDocGenTest {
@Test
public void getterGenerationTest() {
String getterJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.GETTER, "testGeneration1");
String getterJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.GETTER, "testGeneration1", false);
assertTrue(getterJavaDoc.contains("Returns the attribute") && getterJavaDoc.contains(" */\n"));
}
......@@ -126,7 +126,7 @@ public final class JavaDocGenTest {
*/
@Test
public void implClassGenerationTest() {
String implClassJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.IMPL_CLASS, "testGeneration1");
String implClassJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.IMPL_CLASS, "testGeneration1", false);
assertTrue(implClassJavaDoc.contains("Provides the implementation of") && implClassJavaDoc.contains(" */\n"));
}
......@@ -136,7 +136,7 @@ public final class JavaDocGenTest {
@Test
public void interfaceGenerationTest() {
String interfaceJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.INTERFACE, "testGeneration1");
String interfaceJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.INTERFACE, "testGeneration1", false);
assertTrue(interfaceJavaDoc.contains("Abstraction of an entity which provides functionalities of")
&& interfaceJavaDoc.contains(" */\n"));
}
......@@ -146,10 +146,9 @@ public final class JavaDocGenTest {
*/
@Test
public void packageInfoGenerationTest() {
// TODO: udpate to new framework.
// String packageInfo = JavaDocGen.getJavaDoc(JavaDocType.PACKAGE_INFO, "testGeneration1");
// assertTrue(packageInfo.contains(
// "Generated java code for the YANG file") && packageInfo.contains(" */\n"));
String packageInfo = JavaDocGen.getJavaDoc(JavaDocType.PACKAGE_INFO, "testGeneration1", false);
assertTrue(packageInfo.contains("Generated java code corresponding to YANG") && packageInfo.contains(" */\n"));
}
/**
......@@ -158,7 +157,17 @@ public final class JavaDocGenTest {
@Test
public void setterGenerationTest() {
String setterJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.SETTER, "testGeneration1");
String setterJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.SETTER, "testGeneration1", false);
assertTrue(setterJavaDoc.contains("Returns the builder object of") && setterJavaDoc.contains(" */\n"));
}
/**
* This test case checks the content received for the typedef setter java doc.
*/
@Test
public void typeDefSetterGenerationTest() {
String typeDefSetter = JavaDocGen.getJavaDoc(JavaDocType.TYPE_DEF_SETTER, "testGeneration1", false);
assertTrue(typeDefSetter.contains("Sets the value of") && typeDefSetter.contains(" */\n"));
}
}
\ No newline at end of file
......
/*
* 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 org.junit.Test;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import org.onosproject.yangutils.utils.io.impl.TempDataStore.TempDataStoreType;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;
import java.util.List;
import org.slf4j.Logger;
import static org.slf4j.LoggerFactory.getLogger;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertNotNull;
/**
* Unit tests for the Tempd data store for its contents.
*/
public final class TempDataStoreTest {
private final Logger log = getLogger(getClass());
private static final String CLASS_NAME = "YANG";
@Rule
public ExpectedException thrown = ExpectedException.none();
/**
* A private constructor is tested.
*
* @throws SecurityException if any security violation is observed.
* @throws NoSuchMethodException if when the method is not found.
* @throws IllegalArgumentException if there is illegal argument found.
* @throws InstantiationException if instantiation is provoked for the private constructor.
* @throws IllegalAccessException if instance is provoked or a method is provoked.
* @throws InvocationTargetException when an exception occurs by the method or constructor.
*/
@Test
public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
InstantiationException, IllegalAccessException, InvocationTargetException {
Class<?>[] classesToConstruct = {TempDataStore.class };
for (Class<?> clazz : classesToConstruct) {
Constructor<?> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
assertNotNull(constructor.newInstance());
}
}
/**
* This test case checks the attribute info that is read and put into the list.
*/
@Test
public void insertAttributeDataTest() throws IOException, ClassNotFoundException, FileNotFoundException {
String attributeData = "attribute content lists this";
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)));
TempDataStoreType.valueOf(TempDataStoreType.ATTRIBUTE.toString());
}
/**
* This test case checks the builder interface that is read and put into the list.
*/
@Test
public void insertBuilderInterfaceMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException {
String builderInterfaceMethodsData = "builder interface methods content lists this";
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)));
}
/**
* This test case checks the builder methods that is read and put into the list.
*/
@Test
public void insertBuilderMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException {
String builderMethodsData = "builder methods content lists this";
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)));
}
/**
* This test case checks the impl methods that is read and put into the list.
*/
@Test
public void insertImplMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException {
String implMethodsData = "impl methods content lists this";
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)));
}
/**
* This test case checks the import methods that is read and put into the list.
*/
@Test
public void insertImportTest() throws IOException, ClassNotFoundException, FileNotFoundException {
String importData = "interface methods content lists this";
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)));
}
/**
* This test case checks the interface methods that is read and put into the list.
*/
@Test
public void insertInterfaceMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException {
String interfaceMethodsData = "interface methods content lists this";
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)));
}
}
\ No newline at end of file
......@@ -84,8 +84,8 @@ public final class YangFileScannerTest {
/**
* Method used for creating multiple directories inside the target file.
*
* @param path where directories should be created.
* @return
* @param path where directories should be created
* @return the directory path that is created
*/
public File createDirectory(String path) {
......
......@@ -37,7 +37,7 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/**
* Unit tests for adding package-info, creating directories, cleaning the folder and to add sources.
* Unit tests for YANG io utils.
*/
public final class YangIoUtilsTest {
......@@ -65,6 +65,20 @@ public final class YangIoUtilsTest {
}
/**
* This test case checks with an additional info in the path.
*/
@Test
public void addPackageInfoWithPathTest() throws IOException {
File dirPath = new File(createPath);
dirPath.mkdirs();
CopyrightHeader.parseCopyrightHeader();
YangIoUtils.addPackageInfo(dirPath, "check1", "src/main/yangmodel/" + createPath);
File filePath = new File(dirPath + File.separator + "package-info.java");
assertThat(filePath.isFile(), is(true));
}
/**
* This test case checks whether the package-info file is created when invalid path is given.
*/
@Test
......@@ -81,12 +95,12 @@ public final class YangIoUtilsTest {
/**
* A private constructor is tested.
*
* @throws SecurityException if any security violation is observed.
* @throws NoSuchMethodException if when the method is not found.
* @throws IllegalArgumentException if there is illegal argument found.
* @throws InstantiationException if instantiation is provoked for the private constructor.
* @throws IllegalAccessException if instance is provoked or a method is provoked.
* @throws InvocationTargetException when an exception occurs by the method or constructor.
* @throws SecurityException if any security violation is observed
* @throws NoSuchMethodException if when the method is not found
* @throws IllegalArgumentException if there is illegal argument found
* @throws InstantiationException if instantiation is provoked for the private constructor
* @throws IllegalAccessException if instance is provoked or a method is provoked
* @throws InvocationTargetException when an exception occurs by the method or constructor
*/
@Test
public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
......@@ -146,4 +160,4 @@ public final class YangIoUtilsTest {
sourceDir.mkdirs();
YangIoUtils.addToSource(sourceDir.toString(), project, context);
}
}
\ No newline at end of file
}
......