Bharat saraswal
Committed by Gerrit Code Review

[ONOS-4149],[ONOS-3909] YANG list translator impl and bug fixes.

Change-Id: Ia1a94142a3a114815766f661ed850bf9cacde66f
Showing 29 changed files with 1791 additions and 1349 deletions
......@@ -16,7 +16,6 @@
package org.onosproject.yangutils.plugin.manager;
import java.io.File;
import java.util.Iterator;
import java.util.List;
......@@ -24,31 +23,34 @@ import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.parser.YangUtilsParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
import org.onosproject.yangutils.utils.UtilConstants;
import org.onosproject.yangutils.utils.io.impl.YangFileScanner;
import org.sonatype.plexus.build.incremental.BuildContext;
import static org.apache.maven.plugins.annotations.LifecyclePhase.GENERATE_SOURCES;
import static org.apache.maven.plugins.annotations.ResolutionScope.COMPILE;
import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.generateJavaCode;
import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_BASE_PKG;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addToSource;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.clean;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.convertPkgToPath;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.copyYangFilesToTarget;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getDirectory;
/**
* ONOS YANG utility maven plugin. Goal of plugin is yang2java Execution phase
* in generate-sources requiresDependencyResolution at compile time.
* ONOS YANG utility maven plugin.
* Goal of plugin is yang2java Execution phase in generate-sources requiresDependencyResolution at compile time.
*/
@Mojo(name = "yang2java", defaultPhase = LifecyclePhase.GENERATE_SOURCES,
requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true)
@Mojo(name = "yang2java", defaultPhase = GENERATE_SOURCES, requiresDependencyResolution = COMPILE,
requiresProject = true)
public class YangUtilManager extends AbstractMojo {
/**
......@@ -87,8 +89,7 @@ public class YangUtilManager extends AbstractMojo {
@Component
private BuildContext context;
private static final String DEFAULT_PKG = File.separator
+ UtilConstants.DEFAULT_BASE_PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
private static final String DEFAULT_PKG = SLASH + convertPkgToPath(DEFAULT_BASE_PKG);
private YangUtilsParser yangUtilsParser = new YangUtilsParserManager();
private String searchDir;
......@@ -116,7 +117,7 @@ public class YangUtilManager extends AbstractMojo {
clean(getDirectory(baseDir, outputDirectory));
searchDir = getDirectory(baseDir, yangFilesDir);
codeGenDir = getDirectory(baseDir, genFilesDir) + File.separator;
codeGenDir = getDirectory(baseDir, genFilesDir) + SLASH;
List<String> yangFiles = YangFileScanner.getYangFiles(searchDir);
Iterator<String> yangFileIterator = yangFiles.iterator();
......@@ -133,7 +134,7 @@ public class YangUtilManager extends AbstractMojo {
}
if (e.getMessage() != null) {
logInfo = logInfo + UtilConstants.NEW_LINE + e.getMessage();
logInfo = logInfo + NEW_LINE + e.getMessage();
}
getLog().info(logInfo);
}
......@@ -143,7 +144,7 @@ public class YangUtilManager extends AbstractMojo {
copyYangFilesToTarget(yangFiles, getDirectory(baseDir, outputDirectory), project);
} catch (Exception e) {
getLog().info(e);
//throw new MojoExecutionException("Exception occured due to " + e.getLocalizedMessage());
throw new MojoExecutionException("Exception occured due to " + e.getLocalizedMessage());
}
}
}
......
......@@ -18,6 +18,7 @@ package org.onosproject.yangutils.translator.tojava;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType;
import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getIsQualifiedAccessOrAddToImportList;
import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
......@@ -246,4 +247,40 @@ public final class JavaAttributeInfo {
return newAttr;
}
/**
* Create an attribute info object corresponding to the passed type def attribute
* information and return it.
*
* @param curNode current data model node for which the java file is being
* generated
* @param attributeType leaf data type
* @param attributeName leaf name
* @param isListAttribute is the current added attribute needs to be a list
* @return AttributeInfo attribute details required to add in temporary
* files
*/
public static JavaAttributeInfo getAttributeInfoOfTypeDef(YangNode curNode,
YangType<?> attributeType, String attributeName,
boolean isListAttribute) {
JavaAttributeInfo newAttr = new JavaAttributeInfo();
/*
* Get the import info corresponding to the attribute for import in
* generated java files or qualified access
*/
JavaQualifiedTypeInfo importInfo = getQualifiedTypeInfoOfLeafAttribute(curNode,
attributeType, attributeName, isListAttribute);
AttributesJavaDataType.addImportInfo(importInfo);
newAttr.setImportInfo(importInfo);
newAttr.setIsQualifiedAccess(getIsQualifiedAccessOrAddToImportList(
curNode, importInfo));
newAttr.setAttributeName(getCamelCase(attributeName));
newAttr.setListAttr(isListAttribute);
newAttr.setImportInfo(importInfo);
newAttr.setAttributeType(attributeType);
return newAttr;
}
}
......
......@@ -15,11 +15,26 @@
*/
package org.onosproject.yangutils.translator.tojava;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import org.onosproject.yangutils.datamodel.YangNode;
import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_CLASS;
import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_PKG;
import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_CLASS;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_PKG;
import static org.onosproject.yangutils.utils.UtilConstants.LIST;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
/**
* Generated Java file can contain imports.
*/
......@@ -49,6 +64,7 @@ public class JavaImportData {
* @return true if any of the attribute needs to be maintained as a list.
*/
public boolean getIfListImported() {
return isListToImport;
}
......@@ -58,6 +74,7 @@ public class JavaImportData {
* @param isList status to mention list is bing imported.
*/
public void setIfListImported(boolean isList) {
isListToImport = isList;
}
......@@ -67,6 +84,7 @@ public class JavaImportData {
* @return the set containing the imported class/interface info
*/
public SortedSet<JavaQualifiedTypeInfo> getImportSet() {
return importSet;
}
......@@ -76,6 +94,7 @@ public class JavaImportData {
* @param importSet the set containing the imported class/interface info
*/
private void setImportSet(SortedSet<JavaQualifiedTypeInfo> importSet) {
this.importSet = importSet;
}
......@@ -95,6 +114,7 @@ public class JavaImportData {
* @return status of new addition of class/interface to the import set
*/
public boolean addImportInfo(YangNode curNode, JavaQualifiedTypeInfo newImportInfo) {
if (!(curNode instanceof HasJavaImportData)) {
throw new RuntimeException("missing import info in data model node");
}
......@@ -108,4 +128,62 @@ public class JavaImportData {
((HasJavaImportData) curNode).getJavaImportData().getImportSet().add(newImportInfo);
return true;
}
/**
* Returns import for class.
*
* @param attr java attribute info
* @return imports for class
*/
public List<String> getImports(JavaAttributeInfo attr) {
String importString;
List<String> imports = new ArrayList<>();
for (JavaQualifiedTypeInfo importInfo : getImportSet()) {
importString = IMPORT;
if (importInfo.getPkgInfo() != EMPTY_STRING && importInfo.getClassInfo() != null
&& importInfo.getPkgInfo() != JAVA_LANG) {
importString = importString + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN
+ NEW_LINE;
imports.add(importString);
}
}
if (attr.isListAttr()) {
imports.add(setImportForList());
}
java.util.Collections.sort(imports);
return imports;
}
/**
* Gets import for hash and equals method.
*
* @return import for hash and equals method
*/
public String getImportForHashAndEquals() {
return IMPORT + JAVA_UTIL_OBJECTS_IMPORT_PKG + PERIOD + JAVA_UTIL_OBJECTS_IMPORT_CLASS;
}
/**
* Gets import for to string method.
*
* @return import for to string method
*/
public String getImportForToString() {
return IMPORT + GOOGLE_MORE_OBJECT_IMPORT_PKG + PERIOD + GOOGLE_MORE_OBJECT_IMPORT_CLASS;
}
/**
* Sets import for to list.
*/
private static String setImportForList() {
return IMPORT + COLLECTION_IMPORTS + PERIOD + LIST + SEMI_COLAN + NEW_LINE;
}
}
......
......@@ -18,19 +18,25 @@ package org.onosproject.yangutils.translator.tojava;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.onosproject.yangutils.datamodel.YangLeaf;
import org.onosproject.yangutils.datamodel.YangLeafList;
import org.onosproject.yangutils.datamodel.YangLeavesHolder;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen;
import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax;
import org.onosproject.yangutils.utils.UtilConstants;
import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
import org.onosproject.yangutils.utils.io.impl.JavaDocGen;
import org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
......@@ -43,8 +49,15 @@ import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoOfLeaf;
import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoOfTypeDef;
import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getCurNodeAsAttributeInParent;
import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateImplClassFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateTypeDefClassFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructor;
......@@ -53,11 +66,30 @@ import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterString;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethod;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethod;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOverRideString;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForTypeDefClass;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterString;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethod;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeDefConstructor;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString;
import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.TYPE_DEF_CONSTRUCTOR;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.TYPE_DEF_SETTER_METHOD;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.clean;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.mergeJavaFiles;
/**
* Provides implementation of java code fragments temporary implementations.
......@@ -71,6 +103,12 @@ public class TempJavaCodeFragmentFiles {
private int generatedTempFiles;
/**
* The variable which guides the types of files generated using
* the generated file types mask.
*/
private int generatedJavaFiles;
/**
* Absolute path where the target java file needs to be generated.
*/
private String absoluteDirPath;
......@@ -81,6 +119,11 @@ public class TempJavaCodeFragmentFiles {
private String generatedJavaClassName;
/**
* File type extension for java classes.
*/
private static final String JAVA_FILE_EXTENSION = ".java";
/**
* File type extension for temporary classes.
*/
private static final String TEMP_FILE_EXTENSION = ".tmp";
......@@ -136,6 +179,56 @@ public class TempJavaCodeFragmentFiles {
private static final String EQUALS_METHOD_FILE_NAME = "Equals";
/**
* File name for interface java file name suffix.
*/
private static final String INTERFACE_FILE_NAME_SUFFIX = EMPTY_STRING;
/**
* File name for builder interface file name suffix.
*/
private static final String BUILDER_INTERFACE_FILE_NAME_SUFFIX = BUILDER + INTERFACE;
/**
* File name for builder class file name suffix.
*/
private static final String BUILDER_CLASS_FILE_NAME_SUFFIX = BUILDER;
/**
* File name for impl class file name suffix.
*/
private static final String IMPL_CLASS_FILE_NAME_SUFFIX = IMPL;
/**
* File name for typedef class file name suffix.
*/
private static final String TYPEDEF_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
/**
* Java file handle for interface file.
*/
private File interfaceJavaFileHandle;
/**
* Java file handle for builder interface file.
*/
private File builderInterfaceJavaFileHandle;
/**
* Java file handle for builder class file.
*/
private File builderClassJavaFileHandle;
/**
* Java file handle for impl class file.
*/
private File implClassJavaFileHandle;
/**
* Java file handle for typedef class file.
*/
private File typedefClassJavaFileHandle;
/**
* Temporary file handle for attribute.
*/
private File attributesTempFileHandle;
......@@ -181,6 +274,16 @@ public class TempJavaCodeFragmentFiles {
private File toStringImplTempFileHandle;
/**
* Java attribute info.
*/
private JavaAttributeInfo newAttrInfo;
/**
* Current YANG node.
*/
private YangNode curYangNode;
/**
* Construct an object of temporary java code fragment.
*
* @param genFileType file generation type
......@@ -188,12 +291,13 @@ public class TempJavaCodeFragmentFiles {
* @param className class name
* @throws IOException when fails to create new file handle
*/
public TempJavaCodeFragmentFiles(int genFileType, String genDir, String className) throws IOException {
public TempJavaCodeFragmentFiles(int genFileType, String genDir, String className)
throws IOException {
generatedTempFiles = 0;
absoluteDirPath = genDir;
generatedJavaClassName = className;
generatedJavaFiles = genFileType;
/**
* Initialize getter when generation file type matches to interface
* mask.
......@@ -234,6 +338,18 @@ public class TempJavaCodeFragmentFiles {
generatedTempFiles |= TO_STRING_IMPL_MASK;
}
/**
* Initialize getterImpl, attributes, hash code, equals and
* to strings when generation file type matches to typeDef class mask.
*/
if ((genFileType & GENERATE_TYPEDEF_CLASS) != 0) {
generatedTempFiles |= ATTRIBUTES_MASK;
generatedTempFiles |= GETTER_FOR_CLASS_MASK;
generatedTempFiles |= HASH_CODE_IMPL_MASK;
generatedTempFiles |= EQUALS_IMPL_MASK;
generatedTempFiles |= TO_STRING_IMPL_MASK;
}
if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) {
setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME));
}
......@@ -272,6 +388,106 @@ public class TempJavaCodeFragmentFiles {
}
/**
* Returns java file handle for interface file.
*
* @return java file handle for interface file
*/
public File getInterfaceJavaFileHandle() {
return interfaceJavaFileHandle;
}
/**
* Sets the java file handle for interface file.
*
* @param interfaceJavaFileHandle java file handle
*/
public void setInterfaceJavaFileHandle(File interfaceJavaFileHandle) {
this.interfaceJavaFileHandle = interfaceJavaFileHandle;
}
/**
* Returns java file handle for builder interface file.
*
* @return java file handle for builder interface file
*/
public File getBuilderInterfaceJavaFileHandle() {
return builderInterfaceJavaFileHandle;
}
/**
* Sets the java file handle for builder interface file.
*
* @param builderInterfaceJavaFileHandle java file handle
*/
public void setBuilderInterfaceJavaFileHandle(File builderInterfaceJavaFileHandle) {
this.builderInterfaceJavaFileHandle = builderInterfaceJavaFileHandle;
}
/**
* Returns java file handle for builder class file.
*
* @return java file handle for builder class file
*/
public File getBuilderClassJavaFileHandle() {
return builderClassJavaFileHandle;
}
/**
* Sets the java file handle for builder class file.
*
* @param builderClassJavaFileHandle java file handle
*/
public void setBuilderClassJavaFileHandle(File builderClassJavaFileHandle) {
this.builderClassJavaFileHandle = builderClassJavaFileHandle;
}
/**
* Returns java file handle for impl class file.
*
* @return java file handle for impl class file
*/
public File getImplClassJavaFileHandle() {
return implClassJavaFileHandle;
}
/**
* Sets the java file handle for impl class file.
*
* @param implClassJavaFileHandle java file handle
*/
public void setImplClassJavaFileHandle(File implClassJavaFileHandle) {
this.implClassJavaFileHandle = implClassJavaFileHandle;
}
/**
* Returns java file handle for typedef class file.
*
* @return java file handle for typedef class file
*/
public File getTypedefClassJavaFileHandle() {
return typedefClassJavaFileHandle;
}
/**
* Sets the java file handle for typedef class file.
*
* @param typedefClassJavaFileHandle java file handle
*/
public void setTypedefClassJavaFileHandle(File typedefClassJavaFileHandle) {
this.typedefClassJavaFileHandle = typedefClassJavaFileHandle;
}
/**
* Returns attribute's temporary file handle.
*
* @return temporary file handle
......@@ -452,6 +668,46 @@ public class TempJavaCodeFragmentFiles {
}
/**
* Returns java attribute info.
*
* @return java attribute info
*/
public JavaAttributeInfo getNewAttrInfo() {
return newAttrInfo;
}
/**
* Sets java attribute info.
*
* @param newAttrInfo java attribute info
*/
public void setNewAttrInfo(JavaAttributeInfo newAttrInfo) {
this.newAttrInfo = newAttrInfo;
}
/**
* Returns current YANG node.
*
* @return current YANG node
*/
public YangNode getCurYangNode() {
return curYangNode;
}
/**
* Sets current YANG node.
*
* @param curYangNode YANG node
*/
public void setCurYangNode(YangNode curYangNode) {
this.curYangNode = curYangNode;
}
/**
* Adds attribute for class.
*
* @param attr attribute info
......@@ -459,7 +715,7 @@ public class TempJavaCodeFragmentFiles {
*/
public void addAttribute(JavaAttributeInfo attr) throws IOException {
appendToFile(getAttributesTempFileHandle(), parseAttribute(attr) + UtilConstants.FOUR_SPACE_INDENTATION);
appendToFile(getAttributesTempFileHandle(), parseAttribute(attr) + FOUR_SPACE_INDENTATION);
}
/**
......@@ -470,20 +726,24 @@ public class TempJavaCodeFragmentFiles {
*/
public void addGetterForInterface(JavaAttributeInfo attr) throws IOException {
appendToFile(getGetterInterfaceTempFileHandle(),
getGetterString(attr) + UtilConstants.NEW_LINE);
appendToFile(getGetterInterfaceTempFileHandle(), getGetterString(attr) + NEW_LINE);
}
/**
* Adds getter method's impl for class.
*
* @param attr attribute info
* @param genFiletype generated file type
* @throws IOException when fails to append to temporary file
*/
public void addGetterImpl(JavaAttributeInfo attr) throws IOException {
public void addGetterImpl(JavaAttributeInfo attr, int genFiletype) throws IOException {
appendToFile(getGetterImplTempFileHandle(),
getOverRideString() + getGetterForClass(attr) + UtilConstants.NEW_LINE);
if ((genFiletype & BUILDER_CLASS_MASK) != 0) {
appendToFile(getGetterImplTempFileHandle(), getOverRideString() + getGetterForClass(attr) + NEW_LINE);
} else {
appendToFile(getGetterImplTempFileHandle(), getJavaDoc(GETTER_METHOD, attr.getAttributeName(), false)
+ getGetterForClass(attr) + NEW_LINE);
}
}
/**
......@@ -495,7 +755,7 @@ public class TempJavaCodeFragmentFiles {
public void addSetterForInterface(JavaAttributeInfo attr) throws IOException {
appendToFile(getSetterInterfaceTempFileHandle(),
getSetterString(attr, generatedJavaClassName) + UtilConstants.NEW_LINE);
getSetterString(attr, generatedJavaClassName) + NEW_LINE);
}
/**
......@@ -507,7 +767,7 @@ public class TempJavaCodeFragmentFiles {
public void addSetterImpl(JavaAttributeInfo attr) throws IOException {
appendToFile(getSetterImplTempFileHandle(),
getOverRideString() + getSetterForClass(attr, generatedJavaClassName) + UtilConstants.NEW_LINE);
getOverRideString() + getSetterForClass(attr, generatedJavaClassName) + NEW_LINE);
}
/**
......@@ -529,7 +789,7 @@ public class TempJavaCodeFragmentFiles {
*/
public String addBuildMethodImpl() throws IOException {
return getBuildString(generatedJavaClassName) + UtilConstants.NEW_LINE;
return getBuildString(generatedJavaClassName) + NEW_LINE;
}
/**
......@@ -546,13 +806,50 @@ public class TempJavaCodeFragmentFiles {
/**
* Adds default constructor for class.
*
* @param modifier modifier for constructor.
* @param toAppend string which need to be appended with the class name
* @return default constructor for class
* @throws IOException when fails to append to file
*/
public String addDefaultConstructor(String modifier, String toAppend) throws IOException {
return NEW_LINE + getDefaultConstructorString(generatedJavaClassName + toAppend, modifier);
}
/**
* Adds typedef constructor for class.
*
* @return typedef constructor for class
* @throws IOException when fails to append to file
*/
public String addTypeDefConstructor() throws IOException {
return NEW_LINE + getJavaDoc(TYPE_DEF_CONSTRUCTOR, generatedJavaClassName, false)
+ getTypeDefConstructor(newAttrInfo, generatedJavaClassName) + NEW_LINE;
}
/**
* Adds default constructor for class.
*
* @return default constructor for class
* @throws IOException when fails to append to file
*/
public String addTypeDefsSetter() throws IOException {
return getJavaDoc(TYPE_DEF_SETTER_METHOD, generatedJavaClassName, false) + getSetterForTypeDefClass(newAttrInfo)
+ NEW_LINE;
}
/**
* Adds default constructor for class.
*
* @return default constructor for class
* @throws IOException when fails to append to file
*/
public String addDefaultConstructor() throws IOException {
public String addOfMethod() throws IOException {
return UtilConstants.NEW_LINE + getDefaultConstructorString(generatedJavaClassName + UtilConstants.BUILDER,
UtilConstants.PUBLIC);
return JavaDocGen.getJavaDoc(JavaDocType.OF_METHOD, generatedJavaClassName, false)
+ getOfMethod(generatedJavaClassName, newAttrInfo);
}
/**
......@@ -563,7 +860,7 @@ public class TempJavaCodeFragmentFiles {
*/
public void addHashCodeMethod(JavaAttributeInfo attr) throws IOException {
appendToFile(getHashCodeImplTempFileHandle(), getHashCodeMethod(attr) + UtilConstants.NEW_LINE);
appendToFile(getHashCodeImplTempFileHandle(), getHashCodeMethod(attr) + NEW_LINE);
}
/**
......@@ -574,7 +871,7 @@ public class TempJavaCodeFragmentFiles {
*/
public void addEqualsMethod(JavaAttributeInfo attr) throws IOException {
appendToFile(getEqualsImplTempFileHandle(), getEqualsMethod(attr) + UtilConstants.NEW_LINE);
appendToFile(getEqualsImplTempFileHandle(), getEqualsMethod(attr) + NEW_LINE);
}
/**
......@@ -585,7 +882,7 @@ public class TempJavaCodeFragmentFiles {
*/
public void addToStringMethod(JavaAttributeInfo attr) throws IOException {
appendToFile(getToStringImplTempFileHandle(), getToStringMethod(attr) + UtilConstants.NEW_LINE);
appendToFile(getToStringImplTempFileHandle(), getToStringMethod(attr) + NEW_LINE);
}
/**
......@@ -614,6 +911,20 @@ public class TempJavaCodeFragmentFiles {
}
/**
* Returns a temporary file handle for the specific file type.
*
* @param fileName file name
* @return temporary file handle
* @throws IOException when fails to create new file handle
*/
private File getJavaFileHandle(String fileName) throws IOException {
createPackage(absoluteDirPath, getJavaFileInfo().getJavaName());
return getFileObject(getDirPath(), fileName, JAVA_FILE_EXTENSION, getJavaFileInfo());
}
/**
* Returns data from the temporary files.
*
* @param file temporary file handle
......@@ -624,11 +935,10 @@ public class TempJavaCodeFragmentFiles {
String path = getTempDirPath();
if (new File(path + file.getName()).exists()) {
return FileSystemUtil.readAppendFile(path + file.getName(), UtilConstants.EMPTY_STRING);
return FileSystemUtil.readAppendFile(path + file.getName(), EMPTY_STRING);
} else {
throw new IOException("Unable to get data from the given "
+ file.getName() + " file for "
+ generatedJavaClassName + UtilConstants.PERIOD);
+ file.getName() + " file for " + generatedJavaClassName + PERIOD);
}
}
......@@ -639,8 +949,8 @@ public class TempJavaCodeFragmentFiles {
*/
private String getTempDirPath() {
return absoluteDirPath.replace(UtilConstants.PERIOD, UtilConstants.SLASH)
+ File.separator + generatedJavaClassName + TEMP_FOLDER_NAME_SUFIX + File.separator;
return absoluteDirPath.replace(PERIOD, UtilConstants.SLASH)
+ SLASH + generatedJavaClassName + TEMP_FOLDER_NAME_SUFIX + SLASH;
}
/**
......@@ -654,7 +964,8 @@ public class TempJavaCodeFragmentFiles {
/*
* TODO: check if this utility needs to be called or move to the caller
*/
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
String attributeName = JavaIdentifierSyntax
.getCamelCase(JavaIdentifierSyntax.getLowerCase(attr.getAttributeName()));
if (attr.isQualifiedName()) {
return getJavaAttributeDefination(attr.getImportInfo().getPkgInfo(), attr.getImportInfo().getClassInfo(),
attributeName, attr.isListAttr());
......@@ -674,7 +985,7 @@ public class TempJavaCodeFragmentFiles {
private void appendToFile(File file, String data) throws IOException {
try {
JavaFileGenerator.insert(file, data);
insertDataIntoJavaFile(file, data);
} catch (IOException ex) {
throw new IOException("failed to write in temp file.");
}
......@@ -778,6 +1089,20 @@ public class TempJavaCodeFragmentFiles {
}
/**
* Adds leaf attributes in generated files.
*
* @param curNode current data model node
* @throws IOException IO operation fail
*/
public void addTypeDefAttributeToTempFiles(YangNode curNode) throws IOException {
JavaAttributeInfo javaAttributeInfo = getAttributeInfoOfTypeDef(curNode,
((YangTypeDef) curNode).getDerivedType().getDataTypeExtendedInfo().getBaseType(),
((YangTypeDef) curNode).getName(), false);
addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
}
/**
* Add the new attribute info to the target generated temporary files.
*
* @param newAttrInfo the attribute info that needs to be added to temporary
......@@ -787,6 +1112,7 @@ public class TempJavaCodeFragmentFiles {
void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo)
throws IOException {
setNewAttrInfo(newAttrInfo);
if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) {
addAttribute(newAttrInfo);
}
......@@ -800,7 +1126,7 @@ public class TempJavaCodeFragmentFiles {
}
if ((generatedTempFiles & GETTER_FOR_CLASS_MASK) != 0) {
addGetterImpl(newAttrInfo);
addGetterImpl(newAttrInfo, generatedJavaFiles);
}
if ((generatedTempFiles & SETTER_FOR_CLASS_MASK) != 0) {
......@@ -826,11 +1152,134 @@ public class TempJavaCodeFragmentFiles {
}
/**
* Return java file info.
*
* @return java file info
*/
private JavaFileInfo getJavaFileInfo() {
return ((HasJavaFileInfo) getCurYangNode()).getJavaFileInfo();
}
/**
* Returns java class name.
*
* @param suffix for the class name based on the file type
* @return java class name
*/
private String getJavaClassName(String suffix) {
return JavaIdentifierSyntax.getCaptialCase(getJavaFileInfo().getJavaName()) + suffix;
}
/**
* Returns the directory path.
*
* @return directory path
*/
private String getDirPath() {
return getJavaFileInfo().getPackageFilePath();
}
/**
* Construct java code exit.
*
* @param fileType generated file type
* @param curNode current YANG node
* @throws IOException when fails to generate java files
*/
public void generateJavaFile(int fileType, YangNode curNode) throws IOException {
setCurYangNode(curNode);
List<String> imports = new ArrayList<>();
if (curNode instanceof HasJavaImportData) {
imports = ((HasJavaImportData) curNode).getJavaImportData().getImports(getNewAttrInfo());
}
/**
* Start generation of files.
*/
if ((fileType & INTERFACE_MASK) != 0 | (fileType & BUILDER_INTERFACE_MASK) != 0
| fileType == GENERATE_INTERFACE_WITH_BUILDER) {
/**
* Create interface file.
*/
setInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX)));
setInterfaceJavaFileHandle(generateInterfaceFile(getInterfaceJavaFileHandle(), imports, curNode));
/**
* Create builder interface file.
*/
setBuilderInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX)));
setBuilderInterfaceJavaFileHandle(
generateBuilderInterfaceFile(getBuilderInterfaceJavaFileHandle(), curNode));
/**
* Append builder interface file to interface file and close it.
*/
mergeJavaFiles(getBuilderInterfaceJavaFileHandle(), getInterfaceJavaFileHandle());
insertDataIntoJavaFile(getInterfaceJavaFileHandle(), JavaCodeSnippetGen.getJavaClassDefClose());
}
if (curNode instanceof HasJavaImportData) {
imports.add(((HasJavaImportData) curNode).getJavaImportData().getImportForHashAndEquals());
imports.add(((HasJavaImportData) curNode).getJavaImportData().getImportForToString());
java.util.Collections.sort(imports);
}
if ((fileType & BUILDER_CLASS_MASK) != 0 | (fileType & IMPL_CLASS_MASK) != 0
| fileType == GENERATE_INTERFACE_WITH_BUILDER) {
/**
* Create builder class file.
*/
setBuilderClassJavaFileHandle(getJavaFileHandle(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX)));
setBuilderClassJavaFileHandle(generateBuilderClassFile(getBuilderClassJavaFileHandle(), imports, curNode));
/**
* Create impl class file.
*/
setImplClassJavaFileHandle(getJavaFileHandle(getJavaClassName(IMPL_CLASS_FILE_NAME_SUFFIX)));
setImplClassJavaFileHandle(generateImplClassFile(getImplClassJavaFileHandle(), curNode));
/**
* Append impl class to builder class and close it.
*/
mergeJavaFiles(getImplClassJavaFileHandle(), getBuilderClassJavaFileHandle());
insertDataIntoJavaFile(getBuilderClassJavaFileHandle(), JavaCodeSnippetGen.getJavaClassDefClose());
}
/**
* Creates type def class file.
*/
if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) {
setTypedefClassJavaFileHandle(getJavaFileHandle(getJavaClassName(TYPEDEF_CLASS_FILE_NAME_SUFFIX)));
setTypedefClassJavaFileHandle(generateTypeDefClassFile(getTypedefClassJavaFileHandle(), curNode, imports));
}
/**
* Close all the file handles.
*/
close();
}
/**
* Removes all temporary file handles.
*
* @throws IOException when failed to delete the temporary files
*/
public void close() throws IOException {
private void close() throws IOException {
closeFile(getJavaClassName(INTERFACE_FILE_NAME_SUFFIX));
closeFile(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX));
getJavaFileHandle(getJavaClassName(BUILDER_INTERFACE_FILE_NAME_SUFFIX)).delete();
closeFile(getJavaClassName(BUILDER_CLASS_FILE_NAME_SUFFIX));
closeFile(getJavaClassName(IMPL_CLASS_FILE_NAME_SUFFIX));
getJavaFileHandle(getJavaClassName(IMPL_CLASS_FILE_NAME_SUFFIX)).delete();
closeFile(getJavaClassName(TYPEDEF_CLASS_FILE_NAME_SUFFIX));
closeFile(GETTER_METHOD_FILE_NAME);
getTemporaryFileHandle(GETTER_METHOD_FILE_NAME).delete();
......@@ -859,6 +1308,7 @@ public class TempJavaCodeFragmentFiles {
closeFile(EQUALS_METHOD_FILE_NAME);
getTemporaryFileHandle(EQUALS_METHOD_FILE_NAME).delete();
clean(getTempDirPath());
}
/**
......
......@@ -27,12 +27,10 @@ import org.onosproject.yangutils.translator.tojava.JavaImportData;
import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
import static org.onosproject.yangutils.translator.tojava.utils.GenerateJavaCodeExitBuilder.generateJavaFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
/**
......@@ -162,7 +160,7 @@ public class YangJavaContainer extends YangContainer
String absloutePath = getAbsolutePackagePath(
getJavaFileInfo().getBaseCodeGenPath(),
getJavaFileInfo().getPackageFilePath());
createPackage(absloutePath, getName());
setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles(
getJavaFileInfo().getGeneratedFileTypes(), absloutePath,
getJavaFileInfo().getJavaName()));
......@@ -180,8 +178,8 @@ public class YangJavaContainer extends YangContainer
@Override
public void generateCodeExit() throws IOException {
generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
getTempJavaCodeFragmentFiles().close();
getTempJavaCodeFragmentFiles().setCurYangNode(this);
getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
}
}
......
......@@ -17,6 +17,7 @@
package org.onosproject.yangutils.translator.tojava.javamodel;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangInput;
import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
import org.onosproject.yangutils.translator.tojava.HasJavaImportData;
......@@ -27,7 +28,6 @@ import org.onosproject.yangutils.translator.tojava.JavaImportData;
import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
import static org.onosproject.yangutils.translator.tojava.utils.GenerateJavaCodeExitBuilder.generateJavaFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
......@@ -180,7 +180,7 @@ public class YangJavaInput extends YangInput
@Override
public void generateCodeExit() throws IOException {
generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
getTempJavaCodeFragmentFiles().close();
getTempJavaCodeFragmentFiles().setCurYangNode(this);
getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
}
}
......
......@@ -27,7 +27,6 @@ import org.onosproject.yangutils.translator.tojava.JavaImportData;
import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
import static org.onosproject.yangutils.translator.tojava.utils.GenerateJavaCodeExitBuilder.generateJavaFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
......@@ -155,9 +154,11 @@ public class YangJavaList extends YangList
getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName())));
getJavaFileInfo().setPackage(getCurNodePackage(this));
getJavaFileInfo().setPackageFilePath(
getPackageDirPathFromJavaJPackage(getJavaFileInfo().getPackage()));
getJavaFileInfo().setBaseCodeGenPath(codeGenDir);
String absloutePath = getAbsolutePackagePath(
getJavaFileInfo().getBaseCodeGenPath(),
getJavaFileInfo().getPackageFilePath());
......@@ -180,7 +181,7 @@ public class YangJavaList extends YangList
@Override
public void generateCodeExit() throws IOException {
generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
getTempJavaCodeFragmentFiles().close();
getTempJavaCodeFragmentFiles().setCurYangNode(this);
getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
}
}
......
......@@ -27,12 +27,10 @@ import org.onosproject.yangutils.translator.tojava.JavaImportData;
import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
import static org.onosproject.yangutils.translator.tojava.utils.GenerateJavaCodeExitBuilder.generateJavaFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
/**
......@@ -163,7 +161,6 @@ public class YangJavaModule extends YangModule
getJavaFileInfo().getBaseCodeGenPath(),
getJavaFileInfo().getPackageFilePath());
createPackage(absloutePath, getName());
setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles(
getJavaFileInfo().getGeneratedFileTypes(), absloutePath,
getJavaFileInfo().getJavaName()));
......@@ -174,8 +171,8 @@ public class YangJavaModule extends YangModule
@Override
public void generateCodeExit() throws IOException {
generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
getTempJavaCodeFragmentFiles().close();
getTempJavaCodeFragmentFiles().setCurYangNode(this);
getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
return;
}
}
......
......@@ -17,6 +17,7 @@
package org.onosproject.yangutils.translator.tojava.javamodel;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangOutput;
import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
import org.onosproject.yangutils.translator.tojava.HasJavaImportData;
......@@ -27,7 +28,6 @@ import org.onosproject.yangutils.translator.tojava.JavaImportData;
import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
import static org.onosproject.yangutils.translator.tojava.utils.GenerateJavaCodeExitBuilder.generateJavaFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
......@@ -180,7 +180,7 @@ public class YangJavaOutput extends YangOutput
@Override
public void generateCodeExit() throws IOException {
generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
getTempJavaCodeFragmentFiles().close();
getTempJavaCodeFragmentFiles().setCurYangNode(this);
getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
}
}
......
......@@ -75,6 +75,7 @@ public class YangJavaSubModule extends YangSubModule
*/
@Override
public JavaFileInfo getJavaFileInfo() {
if (javaFileInfo == null) {
throw new RuntimeException("Missing java info in java datamodel node");
}
......@@ -88,6 +89,7 @@ public class YangJavaSubModule extends YangSubModule
*/
@Override
public void setJavaFileInfo(JavaFileInfo javaInfo) {
javaFileInfo = javaInfo;
}
......@@ -98,6 +100,7 @@ public class YangJavaSubModule extends YangSubModule
*/
@Override
public JavaImportData getJavaImportData() {
return javaImportData;
}
......@@ -109,6 +112,7 @@ public class YangJavaSubModule extends YangSubModule
*/
@Override
public void setJavaImportData(JavaImportData javaImportData) {
this.javaImportData = javaImportData;
}
......@@ -119,6 +123,7 @@ public class YangJavaSubModule extends YangSubModule
*/
@Override
public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
if (tempFileHandle == null) {
throw new RuntimeException("missing temp file hand for current node "
+ getJavaFileInfo().getJavaName());
......@@ -133,6 +138,7 @@ public class YangJavaSubModule extends YangSubModule
*/
@Override
public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
tempFileHandle = fileHandle;
}
......@@ -144,6 +150,7 @@ public class YangJavaSubModule extends YangSubModule
* @return the name space string of the module.
*/
private String getNameSpaceFromModule(YangBelongsTo belongsToInfo) {
// TODO Auto-generated method stub
return "";
}
......@@ -157,6 +164,7 @@ public class YangJavaSubModule extends YangSubModule
*/
@Override
public void generateCodeEntry(String codeGenDir) throws IOException {
getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName())));
getJavaFileInfo().setPackage(getRootPackage(getVersion(),
getNameSpaceFromModule(getBelongsTo()),
......
......@@ -15,24 +15,29 @@
*/
package org.onosproject.yangutils.translator.tojava.javamodel;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
import org.onosproject.yangutils.translator.tojava.HasJavaImportData;
import org.onosproject.yangutils.translator.tojava.HasTempJavaCodeFragmentFiles;
import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
import org.onosproject.yangutils.translator.tojava.JavaImportData;
import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
/**
* Type define information extended to support java code generation.
*/
public class YangJavaTypeDef extends YangTypeDef
implements JavaCodeGenerator, HasJavaFileInfo, HasJavaImportData {
implements JavaCodeGenerator, HasJavaFileInfo, HasJavaImportData, HasTempJavaCodeFragmentFiles {
/**
* Contains the information of the java file being generated.
......@@ -46,13 +51,19 @@ public class YangJavaTypeDef extends YangTypeDef
private JavaImportData javaImportData;
/**
* File handle to maintain temporary java code fragments as per the code
* snippet types.
*/
private TempJavaCodeFragmentFiles tempFileHandle;
/**
* Default constructor.
*/
public YangJavaTypeDef() {
super();
setJavaFileInfo(new JavaFileInfo());
setJavaImportData(new JavaImportData());
getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
getJavaFileInfo().setGeneratedFileTypes(GENERATE_TYPEDEF_CLASS);
}
/**
......@@ -62,6 +73,7 @@ public class YangJavaTypeDef extends YangTypeDef
*/
@Override
public JavaFileInfo getJavaFileInfo() {
if (javaFileInfo == null) {
throw new RuntimeException("Missing java info in java datamodel node");
}
......@@ -75,6 +87,7 @@ public class YangJavaTypeDef extends YangTypeDef
*/
@Override
public void setJavaFileInfo(JavaFileInfo javaInfo) {
javaFileInfo = javaInfo;
}
......@@ -85,6 +98,7 @@ public class YangJavaTypeDef extends YangTypeDef
*/
@Override
public JavaImportData getJavaImportData() {
return javaImportData;
}
......@@ -96,32 +110,73 @@ public class YangJavaTypeDef extends YangTypeDef
*/
@Override
public void setJavaImportData(JavaImportData javaImportData) {
this.javaImportData = javaImportData;
}
/**
* Get the temporary file handle.
*
* @return temporary file handle
*/
@Override
public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
if (tempFileHandle == null) {
throw new RuntimeException("missing temp file hand for current node "
+ getJavaFileInfo().getJavaName());
}
return tempFileHandle;
}
/**
* Set temporary file handle.
*
* @param fileHandle temporary file handle
*/
@Override
public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
tempFileHandle = fileHandle;
}
/**
* Prepare the information for java code generation corresponding to YANG
* container info.
*
* @param codeGenDir code generation directory
* @throws IOException IO operations fails
*/
@Override
public void generateCodeEntry(String codeGenDir) {
public void generateCodeEntry(String codeGenDir) throws IOException {
getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName())));
getJavaFileInfo().setPackage(getCurNodePackage(this));
getJavaFileInfo().setPackageFilePath(
getPackageDirPathFromJavaJPackage(getJavaFileInfo().getPackage()));
getJavaFileInfo().setBaseCodeGenPath(codeGenDir);
// TODO: generate type define temporary files
String absloutePath = getAbsolutePackagePath(
getJavaFileInfo().getBaseCodeGenPath(),
getJavaFileInfo().getPackageFilePath());
setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles(
getJavaFileInfo().getGeneratedFileTypes(), absloutePath,
getJavaFileInfo().getJavaName()));
getTempJavaCodeFragmentFiles().addTypeDefAttributeToTempFiles(this);
}
/**
* Create a java file using the YANG grouping info.
* @throws IOException IO operations fails
*/
@Override
public void generateCodeExit() {
// TODO Auto-generated method stub
public void generateCodeExit() throws IOException {
getTempJavaCodeFragmentFiles().setCurYangNode(this);
getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_TYPEDEF_CLASS, this);
}
}
......
......@@ -22,7 +22,41 @@ import java.util.TreeSet;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.onosproject.yangutils.datamodel.YangDataTypes.BINARY;
import static org.onosproject.yangutils.datamodel.YangDataTypes.BITS;
import static org.onosproject.yangutils.datamodel.YangDataTypes.BOOLEAN;
import static org.onosproject.yangutils.datamodel.YangDataTypes.DECIMAL64;
import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED;
import static org.onosproject.yangutils.datamodel.YangDataTypes.EMPTY;
import static org.onosproject.yangutils.datamodel.YangDataTypes.ENUMERATION;
import static org.onosproject.yangutils.datamodel.YangDataTypes.IDENTITYREF;
import static org.onosproject.yangutils.datamodel.YangDataTypes.INSTANCE_IDENTIFIER;
import static org.onosproject.yangutils.datamodel.YangDataTypes.INT16;
import static org.onosproject.yangutils.datamodel.YangDataTypes.INT32;
import static org.onosproject.yangutils.datamodel.YangDataTypes.INT64;
import static org.onosproject.yangutils.datamodel.YangDataTypes.INT8;
import static org.onosproject.yangutils.datamodel.YangDataTypes.LEAFREF;
import static org.onosproject.yangutils.datamodel.YangDataTypes.STRING;
import static org.onosproject.yangutils.datamodel.YangDataTypes.UINT16;
import static org.onosproject.yangutils.datamodel.YangDataTypes.UINT32;
import static org.onosproject.yangutils.datamodel.YangDataTypes.UINT64;
import static org.onosproject.yangutils.datamodel.YangDataTypes.UINT8;
import static org.onosproject.yangutils.datamodel.YangDataTypes.UNION;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
import static org.onosproject.yangutils.utils.UtilConstants.BOOLEAN_DATA_TYPE;
import static org.onosproject.yangutils.utils.UtilConstants.BOOLEAN_WRAPPER;
import static org.onosproject.yangutils.utils.UtilConstants.BYTE;
import static org.onosproject.yangutils.utils.UtilConstants.BYTE_WRAPPER;
import static org.onosproject.yangutils.utils.UtilConstants.INT;
import static org.onosproject.yangutils.utils.UtilConstants.INTEGER_WRAPPER;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
import static org.onosproject.yangutils.utils.UtilConstants.LONG;
import static org.onosproject.yangutils.utils.UtilConstants.LONG_WRAPPER;
import static org.onosproject.yangutils.utils.UtilConstants.SHORT;
import static org.onosproject.yangutils.utils.UtilConstants.SHORT_WRAPPER;
import static org.onosproject.yangutils.utils.UtilConstants.STRING_DATA_TYPE;
/**
* Provides java data types corresponding to YANG type.
......@@ -43,6 +77,7 @@ public final class AttributesJavaDataType {
* @return import info
*/
public static Set<JavaQualifiedTypeInfo> getImportInfo() {
return importInfo;
}
......@@ -52,6 +87,7 @@ public final class AttributesJavaDataType {
* @param importData import info
*/
public static void addImportInfo(JavaQualifiedTypeInfo importData) {
getImportInfo().add(importData);
}
......@@ -62,47 +98,48 @@ public final class AttributesJavaDataType {
* @return java type
*/
public static String getJavaDataType(YangType<?> yangType) {
YangDataTypes type = yangType.getDataType();
if (type.equals(YangDataTypes.INT8)) {
return UtilConstants.BYTE;
} else if (type.equals(YangDataTypes.INT16)) {
return UtilConstants.SHORT;
} else if (type.equals(YangDataTypes.INT32)) {
return UtilConstants.INT;
} else if (type.equals(YangDataTypes.INT64)) {
return UtilConstants.LONG;
} else if (type.equals(YangDataTypes.UINT8)) {
return UtilConstants.SHORT;
} else if (type.equals(YangDataTypes.UINT16)) {
return UtilConstants.INT;
} else if (type.equals(YangDataTypes.UINT32)) {
return UtilConstants.LONG;
} else if (type.equals(YangDataTypes.UINT64)) {
if (type.equals(INT8)) {
return BYTE;
} else if (type.equals(INT16)) {
return SHORT;
} else if (type.equals(INT32)) {
return INT;
} else if (type.equals(INT64)) {
return LONG;
} else if (type.equals(UINT8)) {
return SHORT;
} else if (type.equals(UINT16)) {
return INT;
} else if (type.equals(UINT32)) {
return LONG;
} else if (type.equals(UINT64)) {
//TODO: BIGINTEGER.
} else if (type.equals(YangDataTypes.DECIMAL64)) {
} else if (type.equals(DECIMAL64)) {
//TODO: DECIMAL64.
} else if (type.equals(YangDataTypes.STRING)) {
return UtilConstants.STRING;
} else if (type.equals(YangDataTypes.BOOLEAN)) {
return UtilConstants.BOOLEAN;
} else if (type.equals(YangDataTypes.ENUMERATION)) {
} else if (type.equals(STRING)) {
return STRING_DATA_TYPE;
} else if (type.equals(BOOLEAN)) {
return BOOLEAN_DATA_TYPE;
} else if (type.equals(ENUMERATION)) {
//TODO: ENUMERATION.
} else if (type.equals(YangDataTypes.BITS)) {
} else if (type.equals(BITS)) {
//TODO:BITS
} else if (type.equals(YangDataTypes.BINARY)) {
} else if (type.equals(BINARY)) {
//TODO:BINARY
} else if (type.equals(YangDataTypes.LEAFREF)) {
} else if (type.equals(LEAFREF)) {
//TODO:LEAFREF
} else if (type.equals(YangDataTypes.IDENTITYREF)) {
} else if (type.equals(IDENTITYREF)) {
//TODO:IDENTITYREF
} else if (type.equals(YangDataTypes.EMPTY)) {
} else if (type.equals(EMPTY)) {
//TODO:EMPTY
} else if (type.equals(YangDataTypes.UNION)) {
} else if (type.equals(UNION)) {
//TODO:UNION
} else if (type.equals(YangDataTypes.INSTANCE_IDENTIFIER)) {
} else if (type.equals(INSTANCE_IDENTIFIER)) {
//TODO:INSTANCE_IDENTIFIER
} else if (type.equals(YangDataTypes.DERIVED)) {
} else if (type.equals(DERIVED)) {
return yangType.getDataTypeName();
}
return null;
......@@ -116,77 +153,76 @@ public final class AttributesJavaDataType {
* @return java import class
*/
public static String getJavaImportClass(YangType<?> yangType, boolean isListAttr) {
YangDataTypes type = yangType.getDataType();
if (isListAttr) {
if (type.equals(YangDataTypes.INT8)) {
return UtilConstants.BYTE_WRAPPER;
} else if (type.equals(YangDataTypes.INT16)) {
return UtilConstants.SHORT_WRAPPER;
} else if (type.equals(YangDataTypes.INT32)) {
return UtilConstants.INTEGER_WRAPPER;
} else if (type.equals(YangDataTypes.INT64)) {
return UtilConstants.LONG_WRAPPER;
} else if (type.equals(YangDataTypes.UINT8)) {
return UtilConstants.SHORT_WRAPPER;
} else if (type.equals(YangDataTypes.UINT16)) {
return UtilConstants.INTEGER_WRAPPER;
} else if (type.equals(YangDataTypes.UINT32)) {
return UtilConstants.LONG_WRAPPER;
} else if (type.equals(YangDataTypes.UINT64)) {
if (type.equals(INT8)) {
return BYTE_WRAPPER;
} else if (type.equals(INT16)) {
return SHORT_WRAPPER;
} else if (type.equals(INT32)) {
return INTEGER_WRAPPER;
} else if (type.equals(INT64)) {
return LONG_WRAPPER;
} else if (type.equals(UINT8)) {
return SHORT_WRAPPER;
} else if (type.equals(UINT16)) {
return INTEGER_WRAPPER;
} else if (type.equals(UINT32)) {
return LONG_WRAPPER;
} else if (type.equals(UINT64)) {
//TODO: BIGINTEGER.
} else if (type.equals(YangDataTypes.DECIMAL64)) {
} else if (type.equals(DECIMAL64)) {
//TODO: DECIMAL64.
} else if (type.equals(YangDataTypes.STRING)) {
return UtilConstants.STRING;
} else if (type.equals(YangDataTypes.BOOLEAN)) {
return UtilConstants.BOOLEAN_WRAPPER;
} else if (type.equals(YangDataTypes.ENUMERATION)) {
} else if (type.equals(STRING)) {
return STRING_DATA_TYPE;
} else if (type.equals(BOOLEAN)) {
return BOOLEAN_WRAPPER;
} else if (type.equals(ENUMERATION)) {
//TODO: ENUMERATION.
} else if (type.equals(YangDataTypes.BITS)) {
} else if (type.equals(BITS)) {
//TODO:BITS
} else if (type.equals(YangDataTypes.BINARY)) {
} else if (type.equals(BINARY)) {
//TODO:BINARY
} else if (type.equals(YangDataTypes.LEAFREF)) {
} else if (type.equals(LEAFREF)) {
//TODO:LEAFREF
} else if (type.equals(YangDataTypes.IDENTITYREF)) {
} else if (type.equals(IDENTITYREF)) {
//TODO:IDENTITYREF
} else if (type.equals(YangDataTypes.EMPTY)) {
} else if (type.equals(EMPTY)) {
//TODO:EMPTY
} else if (type.equals(YangDataTypes.UNION)) {
} else if (type.equals(UNION)) {
//TODO:UNION
} else if (type.equals(YangDataTypes.INSTANCE_IDENTIFIER)) {
} else if (type.equals(INSTANCE_IDENTIFIER)) {
//TODO:INSTANCE_IDENTIFIER
} else if (type.equals(YangDataTypes.DERIVED)) {
return JavaIdentifierSyntax
.getCaptialCase(JavaIdentifierSyntax.getCamelCase(yangType.getDataTypeName()));
} else if (type.equals(DERIVED)) {
return getCaptialCase(getCamelCase(yangType.getDataTypeName()));
}
} else {
if (type.equals(YangDataTypes.UINT64)) {
if (type.equals(UINT64)) {
//TODO: BIGINTEGER.
} else if (type.equals(YangDataTypes.DECIMAL64)) {
} else if (type.equals(DECIMAL64)) {
//TODO: DECIMAL64.
} else if (type.equals(YangDataTypes.STRING)) {
return UtilConstants.STRING;
} else if (type.equals(YangDataTypes.ENUMERATION)) {
} else if (type.equals(STRING)) {
return STRING_DATA_TYPE;
} else if (type.equals(ENUMERATION)) {
//TODO: ENUMERATION.
} else if (type.equals(YangDataTypes.BITS)) {
} else if (type.equals(BITS)) {
//TODO:BITS
} else if (type.equals(YangDataTypes.BINARY)) {
} else if (type.equals(BINARY)) {
//TODO:BINARY
} else if (type.equals(YangDataTypes.LEAFREF)) {
} else if (type.equals(LEAFREF)) {
//TODO:LEAFREF
} else if (type.equals(YangDataTypes.IDENTITYREF)) {
} else if (type.equals(IDENTITYREF)) {
//TODO:IDENTITYREF
} else if (type.equals(YangDataTypes.EMPTY)) {
} else if (type.equals(EMPTY)) {
//TODO:EMPTY
} else if (type.equals(YangDataTypes.UNION)) {
} else if (type.equals(UNION)) {
//TODO:UNION
} else if (type.equals(YangDataTypes.INSTANCE_IDENTIFIER)) {
} else if (type.equals(INSTANCE_IDENTIFIER)) {
//TODO:INSTANCE_IDENTIFIER
} else if (type.equals(YangDataTypes.DERIVED)) {
return JavaIdentifierSyntax
.getCaptialCase(JavaIdentifierSyntax.getCamelCase(yangType.getDataTypeName()));
} else if (type.equals(DERIVED)) {
return getCaptialCase(getCamelCase(yangType.getDataTypeName()));
}
}
return null;
......@@ -201,40 +237,41 @@ public final class AttributesJavaDataType {
* @return java import package
*/
public static String getJavaImportPackage(YangType<?> yangType, boolean isListAttr, String classInfo) {
YangDataTypes type = yangType.getDataType();
if (isListAttr) {
if (type.equals(YangDataTypes.INT8)
|| type.equals(YangDataTypes.INT16)
|| type.equals(YangDataTypes.INT32)
|| type.equals(YangDataTypes.INT64)
|| type.equals(YangDataTypes.UINT8)
|| type.equals(YangDataTypes.UINT16)
|| type.equals(YangDataTypes.UINT32)
|| type.equals(YangDataTypes.STRING)
|| type.equals(YangDataTypes.BOOLEAN)) {
return UtilConstants.JAVA_LANG;
} else if (type.equals(YangDataTypes.UINT64)) {
if (type.equals(INT8)
|| type.equals(INT16)
|| type.equals(INT32)
|| type.equals(INT64)
|| type.equals(UINT8)
|| type.equals(UINT16)
|| type.equals(UINT32)
|| type.equals(STRING)
|| type.equals(BOOLEAN)) {
return JAVA_LANG;
} else if (type.equals(UINT64)) {
//TODO: BIGINTEGER.
} else if (type.equals(YangDataTypes.DECIMAL64)) {
} else if (type.equals(DECIMAL64)) {
//TODO: DECIMAL64.
} else if (type.equals(YangDataTypes.ENUMERATION)) {
} else if (type.equals(ENUMERATION)) {
//TODO: ENUMERATION.
} else if (type.equals(YangDataTypes.BITS)) {
} else if (type.equals(BITS)) {
//TODO:BITS
} else if (type.equals(YangDataTypes.BINARY)) {
} else if (type.equals(BINARY)) {
//TODO:BINARY
} else if (type.equals(YangDataTypes.LEAFREF)) {
} else if (type.equals(LEAFREF)) {
//TODO:LEAFREF
} else if (type.equals(YangDataTypes.IDENTITYREF)) {
} else if (type.equals(IDENTITYREF)) {
//TODO:IDENTITYREF
} else if (type.equals(YangDataTypes.EMPTY)) {
} else if (type.equals(EMPTY)) {
//TODO:EMPTY
} else if (type.equals(YangDataTypes.UNION)) {
} else if (type.equals(UNION)) {
//TODO:UNION
} else if (type.equals(YangDataTypes.INSTANCE_IDENTIFIER)) {
} else if (type.equals(INSTANCE_IDENTIFIER)) {
//TODO:INSTANCE_IDENTIFIER
} else if (type.equals(YangDataTypes.DERIVED)) {
} else if (type.equals(DERIVED)) {
for (JavaQualifiedTypeInfo imports : getImportInfo()) {
if (imports.getClassInfo().equals(classInfo)) {
return imports.getPkgInfo();
......@@ -243,29 +280,29 @@ public final class AttributesJavaDataType {
}
} else {
if (type.equals(YangDataTypes.UINT64)) {
if (type.equals(UINT64)) {
//TODO: BIGINTEGER.
} else if (type.equals(YangDataTypes.DECIMAL64)) {
} else if (type.equals(DECIMAL64)) {
//TODO: DECIMAL64.
} else if (type.equals(YangDataTypes.STRING)) {
return UtilConstants.JAVA_LANG;
} else if (type.equals(YangDataTypes.ENUMERATION)) {
} else if (type.equals(STRING)) {
return JAVA_LANG;
} else if (type.equals(ENUMERATION)) {
//TODO: ENUMERATION.
} else if (type.equals(YangDataTypes.BITS)) {
} else if (type.equals(BITS)) {
//TODO:BITS
} else if (type.equals(YangDataTypes.BINARY)) {
} else if (type.equals(BINARY)) {
//TODO:BINARY
} else if (type.equals(YangDataTypes.LEAFREF)) {
} else if (type.equals(LEAFREF)) {
//TODO:LEAFREF
} else if (type.equals(YangDataTypes.IDENTITYREF)) {
} else if (type.equals(IDENTITYREF)) {
//TODO:IDENTITYREF
} else if (type.equals(YangDataTypes.EMPTY)) {
} else if (type.equals(EMPTY)) {
//TODO:EMPTY
} else if (type.equals(YangDataTypes.UNION)) {
} else if (type.equals(UNION)) {
//TODO:UNION
} else if (type.equals(YangDataTypes.INSTANCE_IDENTIFIER)) {
} else if (type.equals(INSTANCE_IDENTIFIER)) {
//TODO:INSTANCE_IDENTIFIER
} else if (type.equals(YangDataTypes.DERIVED)) {
} else if (type.equals(DERIVED)) {
for (JavaQualifiedTypeInfo imports : getImportInfo()) {
if (imports.getClassInfo().equals(classInfo)) {
return imports.getPkgInfo();
......
......@@ -16,8 +16,22 @@
package org.onosproject.yangutils.translator.tojava.utils;
import org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
import static org.onosproject.yangutils.utils.UtilConstants.CLASS;
import static org.onosproject.yangutils.utils.UtilConstants.FINAL;
import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
import static org.onosproject.yangutils.utils.UtilConstants.IMPLEMENTS;
import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
/**
* Generates class definition for generated files.
......@@ -44,19 +58,19 @@ public final class ClassDefinitionGenerator {
* based on the file type and the YANG name of the file, generate the
* class / interface definition start.
*/
if ((genFileTypes & GeneratedJavaFileType.INTERFACE_MASK) != 0) {
if ((genFileTypes & INTERFACE_MASK) != 0) {
return getInterfaceDefinition(yangName);
} else if ((genFileTypes & GeneratedJavaFileType.BUILDER_CLASS_MASK) != 0) {
} else if ((genFileTypes & BUILDER_CLASS_MASK) != 0) {
return getBuilderClassDefinition(yangName);
} else if ((genFileTypes & GeneratedJavaFileType.IMPL_CLASS_MASK) != 0) {
} else if ((genFileTypes & IMPL_CLASS_MASK) != 0) {
return getImplClassDefinition(yangName);
} else if ((genFileTypes & GeneratedJavaFileType.BUILDER_INTERFACE_MASK) != 0) {
} else if ((genFileTypes & BUILDER_INTERFACE_MASK) != 0) {
return getBuilderInterfaceDefinition(yangName);
} else if ((genFileTypes & GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS) != 0) {
} else if ((genFileTypes & GENERATE_TYPEDEF_CLASS) != 0) {
return getTypeDefClassDefinition(yangName);
}
......@@ -71,8 +85,7 @@ public final class ClassDefinitionGenerator {
*/
private static String getInterfaceDefinition(String yangName) {
return UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.INTERFACE + UtilConstants.SPACE + yangName
+ UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE;
return PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
}
/**
......@@ -83,8 +96,8 @@ public final class ClassDefinitionGenerator {
* @return definition
*/
private static String getBuilderInterfaceDefinition(String yangName) {
return UtilConstants.INTERFACE + UtilConstants.SPACE + yangName + UtilConstants.BUILDER + UtilConstants.SPACE
+ UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.NEW_LINE;
return INTERFACE + SPACE + yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + NEW_LINE;
}
/**
......@@ -95,10 +108,8 @@ public final class ClassDefinitionGenerator {
*/
private static String getBuilderClassDefinition(String yangName) {
return UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.CLASS + UtilConstants.SPACE + yangName
+ UtilConstants.BUILDER + UtilConstants.SPACE + UtilConstants.IMPLEMENTS + UtilConstants.SPACE
+ yangName + UtilConstants.PERIOD + yangName + UtilConstants.BUILDER + UtilConstants.SPACE
+ UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE;
return PUBLIC + SPACE + CLASS + SPACE + yangName + BUILDER + SPACE + IMPLEMENTS + SPACE + yangName + PERIOD
+ yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
}
/**
......@@ -109,10 +120,8 @@ public final class ClassDefinitionGenerator {
*/
private static String getImplClassDefinition(String yangName) {
return UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.FINAL + UtilConstants.SPACE
+ UtilConstants.CLASS + UtilConstants.SPACE + yangName + UtilConstants.IMPL + UtilConstants.SPACE
+ UtilConstants.IMPLEMENTS + UtilConstants.SPACE + yangName + UtilConstants.SPACE
+ UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE;
return PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + yangName + IMPL + SPACE + IMPLEMENTS + SPACE + yangName
+ SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
}
/**
......@@ -123,9 +132,6 @@ public final class ClassDefinitionGenerator {
*/
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.NEW_LINE;
return PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + 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;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
import org.onosproject.yangutils.translator.tojava.HasJavaImportData;
import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
import org.onosproject.yangutils.translator.tojava.JavaImportData;
import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.appendFileContents;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.clean;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.closeFileHandles;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateImplClassFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.getFileObject;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.insert;
/**
* Provides generate java code exit.
*/
public final class GenerateJavaCodeExitBuilder {
private static final String JAVA_FILE_EXTENSION = ".java";
/**
* Default constructor.
*/
private GenerateJavaCodeExitBuilder() {
}
/**
* Sets import for hash and equals method.
*
* @return import string for implementing hash and equals
*/
private static String setImportForHashAndEquals() {
return UtilConstants.IMPORT + UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_PKG + UtilConstants.PERIOD
+ UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_CLASS;
}
/**
* Sets import for to string method.
*
* @return import string for implementing to string
*/
private static String setImportForToString() {
return UtilConstants.IMPORT + UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_PKG + UtilConstants.PERIOD
+ UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_CLASS;
}
/**
* Sets import for to list.
*
* @return import string for list collection
*/
private static String setImportForList() {
return UtilConstants.IMPORT + UtilConstants.COLLECTION_IMPORTS + UtilConstants.PERIOD
+ UtilConstants.LIST + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE;
}
/**
* Construct java code exit.
*
* @param fileType generated file type
* @param curNode current YANG node
* @throws IOException when fails to generate java files
*/
public static void generateJavaFile(int fileType, YangNode curNode) throws IOException {
JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo();
String className = JavaIdentifierSyntax.getCaptialCase(javaFileInfo.getJavaName());
String pkg = javaFileInfo.getPackageFilePath();
List<String> imports = getImports(((HasJavaImportData) curNode).getJavaImportData());
/**
* Start generation of files.
*/
if ((fileType & INTERFACE_MASK) != 0 | (fileType & BUILDER_INTERFACE_MASK) != 0
| fileType == GENERATE_INTERFACE_WITH_BUILDER) {
/**
* Create interface file.
*/
String interfaceFileName = className;
File interfaceFile = getFileObject(pkg, interfaceFileName, JAVA_FILE_EXTENSION, javaFileInfo);
interfaceFile = generateInterfaceFile(interfaceFile, imports, curNode);
/**
* Create temp builder interface file.
*/
String builderInterfaceFileName = className
+ UtilConstants.BUILDER + UtilConstants.INTERFACE;
File builderInterfaceFile = getFileObject(pkg, builderInterfaceFileName, JAVA_FILE_EXTENSION, javaFileInfo);
builderInterfaceFile = generateBuilderInterfaceFile(builderInterfaceFile, curNode);
/**
* Append builder interface file to interface file and close it.
*/
appendFileContents(builderInterfaceFile, interfaceFile);
insert(interfaceFile, JavaCodeSnippetGen.getJavaClassDefClose());
/**
* Close file handle for interface files.
*/
closeFileHandles(builderInterfaceFile);
closeFileHandles(interfaceFile);
/**
* Remove temp files.
*/
clean(builderInterfaceFile);
}
imports.add(setImportForHashAndEquals());
imports.add(setImportForToString());
java.util.Collections.sort(imports);
if ((fileType & BUILDER_CLASS_MASK) != 0 | (fileType & IMPL_CLASS_MASK) != 0
| fileType == GENERATE_INTERFACE_WITH_BUILDER) {
/**
* Create builder class file.
*/
String builderFileName = className
+ UtilConstants.BUILDER;
File builderFile = getFileObject(pkg, builderFileName, JAVA_FILE_EXTENSION, javaFileInfo);
builderFile = generateBuilderClassFile(builderFile, imports, curNode);
/**
* Create temp impl class file.
*/
String implFileName = className + UtilConstants.IMPL;
File implTempFile = getFileObject(pkg, implFileName, JAVA_FILE_EXTENSION, javaFileInfo);
implTempFile = generateImplClassFile(implTempFile, curNode);
/**
* Append impl class to builder class and close it.
*/
appendFileContents(implTempFile, builderFile);
insert(builderFile, JavaCodeSnippetGen.getJavaClassDefClose());
/**
* Close file handle for classes files.
*/
closeFileHandles(implTempFile);
closeFileHandles(builderFile);
/**
* Remove temp files.
*/
clean(implTempFile);
}
/**
* if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) {
*
* /** Create builder class file. //
*/
//String typeDefFileName = className;
//File typeDefFile = JavaFileGenerator.getFileObject(path, typeDefFileName, JAVA_FILE_EXTENSION,
// ((HasJavaFileInfo) curNode).getJavaFileInfo());
//typeDefFile = JavaFileGenerator.generateTypeDefClassFile(typeDefFile, className, imports,
// path.replace('/', '.'), attrList, ((HasJavaFileInfo) curNode).getJavaFileInfo());
// JavaFileGenerator.insert(typeDefFile, JavaCodeSnippetGen.getJavaClassDefClose());
// /**
// * Close file handle for classes files.
// */
// JavaFileGenerator.closeFileHandles(typeDefFile);
// }
//
}
/**
* Returns import for class.
*
* @param javaImportData import data
* @return imports for class
*/
private static List<String> getImports(JavaImportData javaImportData) {
String importString;
List<String> imports = new ArrayList<>();
for (JavaQualifiedTypeInfo importInfo : javaImportData.getImportSet()) {
importString = UtilConstants.IMPORT;
if (importInfo.getPkgInfo() != "" && importInfo.getClassInfo() != null
&& importInfo.getPkgInfo() != UtilConstants.JAVA_LANG) {
importString = importString + importInfo.getPkgInfo() + ".";
importString = importString + importInfo.getClassInfo() + UtilConstants.SEMI_COLAN
+ UtilConstants.NEW_LINE;
imports.add(importString);
}
}
if (javaImportData.getIfListImported()) {
imports.add(setImportForList());
}
java.util.Collections.sort(imports);
return imports;
}
}
......@@ -17,7 +17,19 @@
package org.onosproject.yangutils.translator.tojava.utils;
import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.onosproject.yangutils.translator.tojava.utils.ClassDefinitionGenerator.generateClassDefinition;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_CLOSE_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_OPEN_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
import static org.onosproject.yangutils.utils.UtilConstants.LIST;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE;
import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
/**
* Utility class to generate the java snippet.
......@@ -52,8 +64,7 @@ public final class JavaCodeSnippetGen {
*/
public static String getImportText(JavaQualifiedTypeInfo importInfo) {
return UtilConstants.IMPORT + importInfo.getPkgInfo() + UtilConstants.PERIOD + importInfo.getClassInfo()
+ UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE;
return IMPORT + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN + NEW_LINE;
}
/**
......@@ -69,8 +80,8 @@ public final class JavaCodeSnippetGen {
/*
* get the camel case name for java class / interface.
*/
yangName = JavaIdentifierSyntax.getCamelCase(yangName);
return ClassDefinitionGenerator.generateClassDefinition(genFileTypes, yangName);
yangName = getCamelCase(yangName);
return generateClassDefinition(genFileTypes, yangName);
}
/**
......@@ -85,30 +96,23 @@ public final class JavaCodeSnippetGen {
public static String getJavaAttributeDefination(String javaAttributeTypePkg, String javaAttributeType,
String javaAttributeName, boolean isList) {
String attributeDefination = UtilConstants.PRIVATE
+ UtilConstants.SPACE;
String attributeDefination = PRIVATE + SPACE;
if (!isList) {
if (javaAttributeTypePkg != null) {
attributeDefination = attributeDefination
+ javaAttributeTypePkg + UtilConstants.PERIOD;
attributeDefination = attributeDefination + javaAttributeTypePkg + PERIOD;
}
attributeDefination = attributeDefination
+ javaAttributeType
+ UtilConstants.SPACE
+ javaAttributeName
+ UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE;
attributeDefination = attributeDefination + javaAttributeType + SPACE + javaAttributeName + SEMI_COLAN
+ NEW_LINE;
} else {
attributeDefination = attributeDefination + UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET;
attributeDefination = attributeDefination + LIST + DIAMOND_OPEN_BRACKET;
if (javaAttributeTypePkg != null) {
attributeDefination = attributeDefination
+ javaAttributeTypePkg + UtilConstants.PERIOD;
attributeDefination = attributeDefination + javaAttributeTypePkg + PERIOD;
}
attributeDefination = attributeDefination
+ javaAttributeType + UtilConstants.DIAMOND_CLOSE_BRACKET + UtilConstants.SPACE
+ javaAttributeName + UtilConstants.SUFIX_S + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE;
attributeDefination = attributeDefination + javaAttributeType + DIAMOND_CLOSE_BRACKET + SPACE
+ javaAttributeName + SEMI_COLAN + NEW_LINE;
}
return attributeDefination;
}
......@@ -121,7 +125,7 @@ public final class JavaCodeSnippetGen {
*/
public static String getListAttribute(String type) {
return UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET + type + UtilConstants.DIAMOND_CLOSE_BRACKET;
return LIST + DIAMOND_OPEN_BRACKET + type + DIAMOND_CLOSE_BRACKET;
}
/**
......@@ -132,7 +136,6 @@ public final class JavaCodeSnippetGen {
*/
public static String getJavaClassDefClose() {
return UtilConstants.CLOSE_CURLY_BRACKET;
return CLOSE_CURLY_BRACKET;
}
}
......
......@@ -21,17 +21,10 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
import org.onosproject.yangutils.translator.tojava.HasTempJavaCodeFragmentFiles;
import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
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 static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
......@@ -47,17 +40,25 @@ import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getDataFromTempFileHandle;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.initiateJavaFileGeneration;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getBuildString;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructorStart;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getDefaultConstructorString;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethodClose;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethodOpen;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethodClose;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethodOpen;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethodClose;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getToStringMethodOpen;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.parseBuilderInterfaceBuildMethodString;
import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE;
import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.partString;
/**
......@@ -72,20 +73,6 @@ public final class JavaFileGenerator {
}
/**
* Returns a file object for generated file.
*
* @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, JavaFileInfo handle) {
return new File(handle.getBaseCodeGenPath() + filePath + File.separator + fileName + extension);
}
/**
* Returns generated interface file for current node.
*
* @param file file
......@@ -101,13 +88,16 @@ public final class JavaFileGenerator {
String className = getCaptialCase(javaFileInfo.getJavaName());
String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath();
initiateFile(file, className, INTERFACE_MASK, imports, path);
initiateJavaFileGeneration(file, className, INTERFACE_MASK, imports, path);
/**
* Add getter methods to interface file.
*/
try {
appendMethod(file, getDataFromTempFileHandle(GETTER_FOR_INTERFACE_MASK, curNode));
/**
* Getter methods.
*/
insertDataIntoJavaFile(file, getDataFromTempFileHandle(GETTER_FOR_INTERFACE_MASK, curNode));
} catch (IOException e) {
throw new IOException("No data found in temporary java code fragment files for " + className
+ " while interface file generation");
......@@ -130,15 +120,19 @@ public final class JavaFileGenerator {
String className = getCaptialCase(javaFileInfo.getJavaName());
String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath();
initiateFile(file, className, BUILDER_INTERFACE_MASK, null, path);
initiateJavaFileGeneration(file, className, BUILDER_INTERFACE_MASK, null, path);
List<String> methods = new ArrayList<>();
try {
methods.add(UtilConstants.FOUR_SPACE_INDENTATION
+ getDataFromTempFileHandle(GETTER_FOR_INTERFACE_MASK, curNode));
methods.add(UtilConstants.NEW_LINE);
methods.add(UtilConstants.FOUR_SPACE_INDENTATION
+ getDataFromTempFileHandle(SETTER_FOR_INTERFACE_MASK, curNode));
/**
* Getter methods.
*/
methods.add(FOUR_SPACE_INDENTATION + getDataFromTempFileHandle(GETTER_FOR_INTERFACE_MASK, curNode));
/**
* Setter methods.
*/
methods.add(NEW_LINE);
methods.add(FOUR_SPACE_INDENTATION + getDataFromTempFileHandle(SETTER_FOR_INTERFACE_MASK, curNode));
} catch (IOException e) {
throw new IOException("No data found in temporary java code fragment files for " + className
+ " while builder interface file generation");
......@@ -147,16 +141,17 @@ public final class JavaFileGenerator {
/**
* Add build method to builder interface file.
*/
methods.add(parseBuilderInterfaceBuildMethodString(className));
methods.add(
((HasTempJavaCodeFragmentFiles) curNode).getTempJavaCodeFragmentFiles().addBuildMethodForInterface());
/**
* Add getters and setters in builder interface.
*/
for (String method : methods) {
appendMethod(file, method);
insertDataIntoJavaFile(file, method);
}
insert(file, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE);
insertDataIntoJavaFile(file, CLOSE_CURLY_BRACKET + NEW_LINE);
return file;
}
......@@ -176,7 +171,7 @@ public final class JavaFileGenerator {
String className = getCaptialCase(javaFileInfo.getJavaName());
String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath();
initiateFile(file, className, BUILDER_CLASS_MASK, imports, path);
initiateJavaFileGeneration(file, className, BUILDER_CLASS_MASK, imports, path);
List<String> methods = new ArrayList<>();
......@@ -184,16 +179,22 @@ public final class JavaFileGenerator {
* Add attribute strings.
*/
try {
insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ getDataFromTempFileHandle(ATTRIBUTES_MASK, curNode));
insertDataIntoJavaFile(file,
NEW_LINE + FOUR_SPACE_INDENTATION + getDataFromTempFileHandle(ATTRIBUTES_MASK, curNode));
} catch (IOException e) {
throw new IOException("No data found in temporary java code fragment files for " + className
+ " while builder class file generation");
}
try {
/**
* Getter methods.
*/
methods.add(getDataFromTempFileHandle(GETTER_FOR_CLASS_MASK, curNode));
methods.add(getDataFromTempFileHandle(SETTER_FOR_CLASS_MASK, curNode) + UtilConstants.NEW_LINE);
/**
* Setter methods.
*/
methods.add(getDataFromTempFileHandle(SETTER_FOR_CLASS_MASK, curNode) + NEW_LINE);
} catch (IOException e) {
throw new IOException("No data found in temporary java code fragment files for " + className
+ " while builder class file generation");
......@@ -202,15 +203,15 @@ public final class JavaFileGenerator {
/**
* Add default constructor and build method impl.
*/
methods.add(getBuildString(className) + UtilConstants.NEW_LINE);
methods.add(UtilConstants.NEW_LINE
+ getDefaultConstructorString(className + UtilConstants.BUILDER, UtilConstants.PUBLIC));
methods.add(((HasTempJavaCodeFragmentFiles) curNode).getTempJavaCodeFragmentFiles().addBuildMethodImpl());
methods.add(((HasTempJavaCodeFragmentFiles) curNode).getTempJavaCodeFragmentFiles()
.addDefaultConstructor(PUBLIC, BUILDER));
/**
* Add methods in builder class.
*/
for (String method : methods) {
appendMethod(file, method);
insertDataIntoJavaFile(file, method);
}
return file;
}
......@@ -231,7 +232,7 @@ public final class JavaFileGenerator {
String className = getCaptialCase(javaFileInfo.getJavaName());
String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath();
initiateFile(file, className, IMPL_CLASS_MASK, null, path);
initiateJavaFileGeneration(file, className, IMPL_CLASS_MASK, null, path);
List<String> methods = new ArrayList<>();
......@@ -239,25 +240,32 @@ public final class JavaFileGenerator {
* Add attribute strings.
*/
try {
insert(file, UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ getDataFromTempFileHandle(ATTRIBUTES_MASK, curNode));
insertDataIntoJavaFile(file,
NEW_LINE + FOUR_SPACE_INDENTATION + getDataFromTempFileHandle(ATTRIBUTES_MASK, curNode));
} catch (IOException e) {
throw new IOException("No data found in temporary java code fragment files for " + className
+ " while impl class file generation");
}
insert(file, UtilConstants.NEW_LINE);
insertDataIntoJavaFile(file, NEW_LINE);
try {
/**
* Getter methods.
*/
methods.add(getDataFromTempFileHandle(GETTER_FOR_CLASS_MASK, curNode));
/**
* Hash code method.
*/
methods.add(getHashCodeMethodClose(getHashCodeMethodOpen() + partString(
getDataFromTempFileHandle(HASH_CODE_IMPL_MASK, curNode).replace(UtilConstants.NEW_LINE,
UtilConstants.EMPTY_STRING))));
methods.add(getEqualsMethodClose(getEqualsMethodOpen(className + UtilConstants.IMPL)
+ getDataFromTempFileHandle(EQUALS_IMPL_MASK, curNode)));
getDataFromTempFileHandle(HASH_CODE_IMPL_MASK, curNode).replace(NEW_LINE, EMPTY_STRING))));
/**
* Equals method.
*/
methods.add(getEqualsMethodClose(
getEqualsMethodOpen(className + IMPL) + getDataFromTempFileHandle(EQUALS_IMPL_MASK, curNode)));
/**
* To string method.
*/
methods.add(getToStringMethodOpen() + getDataFromTempFileHandle(TO_STRING_IMPL_MASK, curNode)
+ getToStringMethodClose());
......@@ -267,8 +275,11 @@ public final class JavaFileGenerator {
}
try {
/**
* Constructor.
*/
methods.add(getConstructorStart(className) + getDataFromTempFileHandle(CONSTRUCTOR_IMPL_MASK, curNode)
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET);
+ FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET);
} catch (IOException e) {
throw new IOException("No data found in temporary java code fragment files for " + className
+ " while impl class file generation");
......@@ -277,262 +288,100 @@ public final class JavaFileGenerator {
* Add methods in impl class.
*/
for (String method : methods) {
appendMethod(file, UtilConstants.FOUR_SPACE_INDENTATION + method + UtilConstants.NEW_LINE);
insertDataIntoJavaFile(file, FOUR_SPACE_INDENTATION + method + NEW_LINE);
}
insert(file, UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE);
insertDataIntoJavaFile(file, CLOSE_CURLY_BRACKET + NEW_LINE);
return file;
}
/**
* Return data stored in temporary files.
*
* @param curNode current YANG node
* @param generatedTempFiles mask for the types of files being generated
* @return data stored in temporary files
* @throws IOException when failed to get the data from temporary file
* handle
*/
private static String getDataFromTempFileHandle(int generatedTempFiles, YangNode curNode) throws IOException {
TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles = ((HasTempJavaCodeFragmentFiles) curNode)
.getTempJavaCodeFragmentFiles();
if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) {
return tempJavaCodeFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getAttributesTempFileHandle());
} else if ((generatedTempFiles & GETTER_FOR_INTERFACE_MASK) != 0) {
return tempJavaCodeFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getGetterInterfaceTempFileHandle());
} else if ((generatedTempFiles & SETTER_FOR_INTERFACE_MASK) != 0) {
return tempJavaCodeFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getSetterInterfaceTempFileHandle());
} else if ((generatedTempFiles & GETTER_FOR_CLASS_MASK) != 0) {
return tempJavaCodeFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getGetterImplTempFileHandle());
} else if ((generatedTempFiles & SETTER_FOR_CLASS_MASK) != 0) {
return tempJavaCodeFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getSetterImplTempFileHandle());
} else if ((generatedTempFiles & CONSTRUCTOR_IMPL_MASK) != 0) {
return tempJavaCodeFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getConstructorImplTempFileHandle());
} else if ((generatedTempFiles & HASH_CODE_IMPL_MASK) != 0) {
return tempJavaCodeFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getHashCodeImplTempFileHandle());
} else if ((generatedTempFiles & EQUALS_IMPL_MASK) != 0) {
return tempJavaCodeFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getEqualsImplTempFileHandle());
} else if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) {
return tempJavaCodeFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getToStringImplTempFileHandle());
}
return null;
}
/**
* Initiate generation of file based on generated file type.
* Generate class file for type def.
*
* @param file generated file
* @param className generated file class name
* @param type generated file type
* @param imports imports for the file
* @param pkg generated file package
* @throws IOException when fails to generate a file
* @param curNode current YANG node
* @param imports imports for file
* @return type def class file
* @throws IOException when fails to generate class file
*/
private static void initiateFile(File file, String className, int type, List<String> imports,
String pkg) throws IOException {
public static File generateTypeDefClassFile(File file, YangNode curNode, List<String> imports) throws IOException {
try {
file.createNewFile();
appendContents(file, className, type, imports, pkg);
} catch (IOException e) {
throw new IOException("Failed to create " + file.getName() + " class file.");
}
}
JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo();
String className = getCaptialCase(javaFileInfo.getJavaName());
String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath();
initiateJavaFileGeneration(file, className, GENERATE_TYPEDEF_CLASS, imports, path);
List<String> methods = new ArrayList<>();
/**
* Appends the temp files to main files.
*
* @param appendFile temp file
* @param srcFile main file
* @throws IOException when fails to append contents
* Add attribute strings.
*/
public static void appendFileContents(File appendFile, File srcFile) throws IOException {
try {
FileSystemUtil.appendFileContents(appendFile, srcFile);
insertDataIntoJavaFile(file,
NEW_LINE + FOUR_SPACE_INDENTATION + getDataFromTempFileHandle(ATTRIBUTES_MASK, curNode));
} catch (IOException e) {
throw new IOException("Failed to append " + appendFile + " in " + srcFile);
}
throw new IOException("No data found in temporary java code fragment files for " + className
+ " while type def class file generation");
}
/**
* Append methods to the generated files.
*
* @param file file in which method needs to be appended
* @param method method which needs to be appended
* @throws IOException IO operation failure
* Default constructor.
*/
private static void appendMethod(File file, String method) throws IOException {
insert(file, method);
}
methods.add(((HasTempJavaCodeFragmentFiles) curNode).getTempJavaCodeFragmentFiles()
.addDefaultConstructor(PRIVATE, EMPTY_STRING));
/**
* Appends all the contents into a generated java file.
*
* @param file generated file
* @param fileName generated file name
* @param type generated file type
* @param pkg generated file package
* @param importsList list of java imports.
* @throws IOException when fails to append contents
* Constructor.
*/
private static void appendContents(File file, String fileName, int type, List<String> importsList,
String pkg) throws IOException {
String pkgString = parsePackageString(pkg, importsList);
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) {
appendHeaderContents(file, pkgString, importsList);
write(file, fileName, type, JavaDocType.IMPL_CLASS);
} else if ((type & INTERFACE_MASK) != 0) {
appendHeaderContents(file, pkgString, importsList);
write(file, fileName, type, JavaDocType.INTERFACE);
} else if ((type & BUILDER_CLASS_MASK) != 0) {
appendHeaderContents(file, pkgString, importsList);
write(file, fileName, type, JavaDocType.BUILDER_CLASS);
}
}
methods.add(((HasTempJavaCodeFragmentFiles) curNode).getTempJavaCodeFragmentFiles()
.addTypeDefConstructor());
/**
* Removes base directory path from package and generates package string for
* file.
*
* @param pkg generated package
* @param importsList list of imports
* @return package string
* Of method.
*/
private static String parsePackageString(String pkg, List<String> importsList) {
if (pkg.contains(UtilConstants.ORG)) {
String[] strArray = pkg.split(UtilConstants.ORG);
pkg = UtilConstants.ORG + strArray[1].replace(UtilConstants.SLASH, UtilConstants.PERIOD);
}
if (importsList != null) {
if (!importsList.isEmpty()) {
return UtilConstants.PACKAGE + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN
+ UtilConstants.NEW_LINE;
} else {
return UtilConstants.PACKAGE + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN;
}
} else {
return UtilConstants.PACKAGE + UtilConstants.SPACE + pkg + UtilConstants.SEMI_COLAN;
}
}
methods.add(((HasTempJavaCodeFragmentFiles) curNode).getTempJavaCodeFragmentFiles().addOfMethod());
try {
/**
* Appends other contents to interface, builder and typedef classes. for
* example : ONOS copyright, imports and package.
*
* @param file generated file
* @param pkg generated package
* @param importsList list of imports
* @throws IOException when fails to append contents.
* Getter method.
*/
private static void appendHeaderContents(File file, String pkg, List<String> importsList) throws IOException {
insert(file, CopyrightHeader.getCopyrightHeader());
insert(file, pkg);
methods.add(getDataFromTempFileHandle(GETTER_FOR_CLASS_MASK, curNode));
/*
* TODO: add the file header using
* JavaCodeSnippetGen.getFileHeaderComment
/**
* Setter method.
*/
if (importsList != null) {
insert(file, UtilConstants.NEW_LINE);
for (String imports : importsList) {
insert(file, imports);
}
}
}
methods.add(((HasTempJavaCodeFragmentFiles) curNode).getTempJavaCodeFragmentFiles().addTypeDefsSetter());
/**
* Write data to the specific generated file.
*
* @param file generated file
* @param fileName file name
* @param genType generated file type
* @param javaDocType java doc type
* @throws IOException when fails to write into a file
* Hash code method.
*/
private static void write(File file, String fileName, int genType, JavaDocGen.JavaDocType javaDocType)
throws IOException {
insert(file, JavaDocGen.getJavaDoc(javaDocType, fileName, false));
insert(file, JavaCodeSnippetGen.getJavaClassDefStart(genType, fileName));
}
methods.add(getHashCodeMethodClose(getHashCodeMethodOpen() + partString(
getDataFromTempFileHandle(HASH_CODE_IMPL_MASK, curNode).replace(NEW_LINE, EMPTY_STRING))));
/**
* Insert in the generated file.
*
* @param file file in which need to be inserted
* @param data data which need to be inserted
* @throws IOException when fails to insert into file
* Equals method.
*/
public static void insert(File file, String data) throws IOException {
try {
FileSystemUtil.updateFileHandle(file, data, false);
} catch (IOException e) {
throw new IOException("Failed to insert in " + file + "file");
}
}
methods.add(getEqualsMethodClose(getEqualsMethodOpen(className + EMPTY_STRING)
+ getDataFromTempFileHandle(EQUALS_IMPL_MASK, curNode)));
/**
* Closes the files handle for generate files.
*
* @param file generate files
* @throws IOException when failed to close the file handle
* To string method.
*/
public static void closeFileHandles(File file) throws IOException {
methods.add(getToStringMethodOpen() + getDataFromTempFileHandle(TO_STRING_IMPL_MASK, curNode)
+ getToStringMethodClose());
try {
FileSystemUtil.updateFileHandle(file, null, true);
} catch (IOException e) {
throw new IOException("Failed to close file handle for " + file + "file");
}
throw new IOException("No data found in temporary java code fragment files for " + className
+ " while tyoe def class file generation");
}
/**
* Removes temp files.
*
* @param file file to be removed
*/
public static void clean(File file) {
if (file.exists()) {
file.delete();
}
for (String method : methods) {
insertDataIntoJavaFile(file, method);
}
insertDataIntoJavaFile(file, CLOSE_CURLY_BRACKET + NEW_LINE);
/**
* 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);
return 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.translator.tojava.utils;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.translator.tojava.HasTempJavaCodeFragmentFiles;
import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
import org.onosproject.yangutils.utils.io.impl.CopyrightHeader;
import org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.HASH_CODE_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefStart;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.ORG;
import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE;
import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILDER_CLASS;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILDER_INTERFACE;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.IMPL_CLASS;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.INTERFACE;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.convertPathToPkg;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
/**
* Provides utilities for java file generator.
*/
public final class JavaFileGeneratorUtils {
/**
* Default constructor.
*/
private JavaFileGeneratorUtils() {
}
/**
* Returns a file object for generated file.
*
* @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, JavaFileInfo handle) {
return new File(handle.getBaseCodeGenPath() + filePath + SLASH + fileName + extension);
}
/**
* Return data stored in temporary files.
*
* @param generatedTempFiles temporary file types
* @param curNode current YANG node
* @return data stored in temporary files
* @throws IOException when failed to get the data from temporary file handle
*/
public static String getDataFromTempFileHandle(int generatedTempFiles, YangNode curNode) throws IOException {
TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles = ((HasTempJavaCodeFragmentFiles) curNode)
.getTempJavaCodeFragmentFiles();
if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) {
return tempJavaCodeFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getAttributesTempFileHandle());
} else if ((generatedTempFiles & GETTER_FOR_INTERFACE_MASK) != 0) {
return tempJavaCodeFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getGetterInterfaceTempFileHandle());
} else if ((generatedTempFiles & SETTER_FOR_INTERFACE_MASK) != 0) {
return tempJavaCodeFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getSetterInterfaceTempFileHandle());
} else if ((generatedTempFiles & GETTER_FOR_CLASS_MASK) != 0) {
return tempJavaCodeFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getGetterImplTempFileHandle());
} else if ((generatedTempFiles & SETTER_FOR_CLASS_MASK) != 0) {
return tempJavaCodeFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getSetterImplTempFileHandle());
} else if ((generatedTempFiles & CONSTRUCTOR_IMPL_MASK) != 0) {
return tempJavaCodeFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getConstructorImplTempFileHandle());
} else if ((generatedTempFiles & HASH_CODE_IMPL_MASK) != 0) {
return tempJavaCodeFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getHashCodeImplTempFileHandle());
} else if ((generatedTempFiles & EQUALS_IMPL_MASK) != 0) {
return tempJavaCodeFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getEqualsImplTempFileHandle());
} else if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) {
return tempJavaCodeFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getToStringImplTempFileHandle());
}
return null;
}
/**
* Initiate generation of file based on generated file type.
*
* @param file generated file
* @param className generated file class name
* @param type generated file type
* @param imports imports for the file
* @param pkg generated file package
* @throws IOException when fails to generate a file
*/
public static void initiateJavaFileGeneration(File file, String className, int type, List<String> imports,
String pkg) throws IOException {
try {
file.createNewFile();
appendContents(file, className, type, imports, pkg);
} catch (IOException e) {
throw new IOException("Failed to create " + file.getName() + " class file.");
}
}
/**
* Appends all the contents into a generated java file.
*
* @param file generated file
* @param fileName generated file name
* @param type generated file type
* @param pkg generated file package
* @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 {
String pkgString = parsePackageString(pkg, importsList);
if ((type & IMPL_CLASS_MASK) != 0) {
write(file, fileName, type, IMPL_CLASS);
} else if ((type & BUILDER_INTERFACE_MASK) != 0) {
write(file, fileName, type, BUILDER_INTERFACE);
} else if ((type & GENERATE_TYPEDEF_CLASS) != 0) {
appendHeaderContents(file, pkgString, importsList);
write(file, fileName, type, IMPL_CLASS);
} else if ((type & INTERFACE_MASK) != 0) {
appendHeaderContents(file, pkgString, importsList);
write(file, fileName, type, INTERFACE);
} else if ((type & BUILDER_CLASS_MASK) != 0) {
appendHeaderContents(file, pkgString, importsList);
write(file, fileName, type, BUILDER_CLASS);
}
}
/**
* Removes base directory path from package and generates package string for file.
*
* @param javaPkg generated java package
* @param importsList list of imports
* @return package string
*/
private static String parsePackageString(String javaPkg, List<String> importsList) {
if (javaPkg.contains(ORG)) {
String[] strArray = javaPkg.split(ORG);
javaPkg = ORG + convertPathToPkg(strArray[1]);
}
if (importsList != null) {
if (!importsList.isEmpty()) {
return PACKAGE + SPACE + javaPkg + SEMI_COLAN + NEW_LINE;
} else {
return PACKAGE + SPACE + javaPkg + SEMI_COLAN;
}
} else {
return PACKAGE + SPACE + javaPkg + SEMI_COLAN;
}
}
/**
* Appends other contents to interface, builder and typedef classes.
* for example : ONOS copyright, imports and package.
* @param file generated file
* @param pkg generated package
* @param importsList list of imports
* @throws IOException when fails to append contents.
*/
private static void appendHeaderContents(File file, String pkg, List<String> importsList) throws IOException {
insertDataIntoJavaFile(file, CopyrightHeader.getCopyrightHeader());
insertDataIntoJavaFile(file, pkg);
/*
* TODO: add the file header using
* JavaCodeSnippetGen.getFileHeaderComment
*/
if (importsList != null) {
insertDataIntoJavaFile(file, NEW_LINE);
for (String imports : importsList) {
insertDataIntoJavaFile(file, imports);
}
}
}
/**
* Write data to the specific generated file.
*
* @param file generated file
* @param fileName file name
* @param genType generated file type
* @param javaDocType java doc type
* @throws IOException when fails to write into a file
*/
private static void write(File file, String fileName, int genType, JavaDocType javaDocType)
throws IOException {
insertDataIntoJavaFile(file, getJavaDoc(javaDocType, fileName, false));
insertDataIntoJavaFile(file, getJavaClassDefStart(genType, fileName));
}
}
......@@ -16,14 +16,24 @@
package org.onosproject.yangutils.translator.tojava.utils;
import java.io.File;
import java.util.ArrayList;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.translator.exception.TranslatorException;
import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.onosproject.yangutils.utils.UtilConstants.COLAN;
import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_BASE_PKG;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.HYPHEN;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_KEY_WORDS;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.QUOTES;
import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_FIRST_DIGIT;
import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_SPECIAL_CHAR;
import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
import static org.onosproject.yangutils.utils.UtilConstants.UNDER_SCORE;
/**
* Utility Class for translating the name from YANG to java convention.
......@@ -53,12 +63,12 @@ public final class JavaIdentifierSyntax {
public static String getRootPackage(byte version, String nameSpace, String revision) {
String pkg;
pkg = UtilConstants.DEFAULT_BASE_PKG;
pkg = pkg + UtilConstants.PERIOD;
pkg = DEFAULT_BASE_PKG;
pkg = pkg + PERIOD;
pkg = pkg + getYangVersion(version);
pkg = pkg + UtilConstants.PERIOD;
pkg = pkg + PERIOD;
pkg = pkg + getPkgFromNameSpace(nameSpace);
pkg = pkg + UtilConstants.PERIOD;
pkg = pkg + PERIOD;
pkg = pkg + getYangRevisionStr(revision);
return pkg.toLowerCase();
......@@ -99,7 +109,7 @@ public final class JavaIdentifierSyntax {
throw new RuntimeException("missing parent java node to get current node's package");
}
JavaFileInfo parentJavaFileHandle = ((HasJavaFileInfo) parentNode).getJavaFileInfo();
pkg = parentJavaFileHandle.getPackage() + UtilConstants.PERIOD + parentJavaFileHandle.getJavaName();
pkg = parentJavaFileHandle.getPackage() + PERIOD + parentJavaFileHandle.getJavaName();
return pkg.toLowerCase();
}
......@@ -123,9 +133,9 @@ public final class JavaIdentifierSyntax {
public static String getPkgFromNameSpace(String nameSpace) {
ArrayList<String> pkgArr = new ArrayList<String>();
nameSpace = nameSpace.replace(UtilConstants.QUOTES, UtilConstants.EMPTY_STRING);
String properNameSpace = nameSpace.replaceAll(UtilConstants.REGEX_WITH_SPECIAL_CHAR, UtilConstants.COLAN);
String[] nameSpaceArr = properNameSpace.split(UtilConstants.COLAN);
nameSpace = nameSpace.replace(QUOTES, EMPTY_STRING);
String properNameSpace = nameSpace.replaceAll(REGEX_WITH_SPECIAL_CHAR, COLAN);
String[] nameSpaceArr = properNameSpace.split(COLAN);
for (String nameSpaceString : nameSpaceArr) {
pkgArr.add(nameSpaceString);
......@@ -142,7 +152,7 @@ public final class JavaIdentifierSyntax {
*/
public static String getYangRevisionStr(String date) throws TranslatorException {
String[] revisionArr = date.split(UtilConstants.HYPHEN);
String[] revisionArr = date.split(HYPHEN);
String rev = "rev";
rev = rev + revisionArr[INDEX_ZERO];
......@@ -172,17 +182,17 @@ public final class JavaIdentifierSyntax {
*/
public static String getPkgFrmArr(ArrayList<String> pkgArr) {
String pkg = UtilConstants.EMPTY_STRING;
String pkg = EMPTY_STRING;
int size = pkgArr.size();
int i = 0;
for (String member : pkgArr) {
boolean presenceOfKeyword = UtilConstants.JAVA_KEY_WORDS.contains(member);
if (presenceOfKeyword || member.matches(UtilConstants.REGEX_FOR_FIRST_DIGIT)) {
member = UtilConstants.UNDER_SCORE + member;
boolean presenceOfKeyword = JAVA_KEY_WORDS.contains(member);
if (presenceOfKeyword || member.matches(REGEX_FOR_FIRST_DIGIT)) {
member = UNDER_SCORE + member;
}
pkg = pkg + member;
if (i != size - 1) {
pkg = pkg + UtilConstants.PERIOD;
pkg = pkg + PERIOD;
}
i++;
}
......@@ -198,7 +208,7 @@ public final class JavaIdentifierSyntax {
public static String getSubPkgFromName(String name) {
ArrayList<String> pkgArr = new ArrayList<String>();
String[] nameArr = name.split(UtilConstants.COLAN);
String[] nameArr = name.split(COLAN);
for (String nameString : nameArr) {
pkgArr.add(nameString);
......@@ -214,7 +224,7 @@ public final class JavaIdentifierSyntax {
*/
public static String getCamelCase(String yangIdentifier) {
String[] strArray = yangIdentifier.split(UtilConstants.HYPHEN);
String[] strArray = yangIdentifier.split(HYPHEN);
String camelCase = strArray[0];
for (int i = 1; i < strArray.length; i++) {
camelCase = camelCase + strArray[i].substring(0, 1).toUpperCase() + strArray[i].substring(1);
......@@ -254,7 +264,7 @@ public final class JavaIdentifierSyntax {
*/
public static String getJavaPackageFromPackagePath(String packagePath) {
return packagePath.replace(File.separator, UtilConstants.PERIOD);
return packagePath.replace(SLASH, PERIOD);
}
/**
......@@ -265,6 +275,6 @@ public final class JavaIdentifierSyntax {
*/
public static String getPackageDirPathFromJavaJPackage(String packagePath) {
return packagePath.replace(UtilConstants.PERIOD, File.separator);
return packagePath.replace(PERIOD, SLASH);
}
}
......
......@@ -17,9 +17,70 @@
package org.onosproject.yangutils.translator.tojava.utils;
import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo;
import org.onosproject.yangutils.utils.UtilConstants;
import org.onosproject.yangutils.utils.io.impl.JavaDocGen;
import org.onosproject.yangutils.utils.io.impl.YangIoUtils;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getLowerCase;
import static org.onosproject.yangutils.utils.UtilConstants.ADD_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.AND;
import static org.onosproject.yangutils.utils.UtilConstants.BOOLEAN_DATA_TYPE;
import static org.onosproject.yangutils.utils.UtilConstants.BUILD;
import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
import static org.onosproject.yangutils.utils.UtilConstants.CHECK_NOT_NULL_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_PARENTHESIS;
import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_CLOSE_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_OPEN_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.EIGHT_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.EQUAL;
import static org.onosproject.yangutils.utils.UtilConstants.EQUALS_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.FALSE;
import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.GET_METHOD_PREFIX;
import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_METHOD_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.HASH;
import static org.onosproject.yangutils.utils.UtilConstants.HASH_CODE_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.IF;
import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
import static org.onosproject.yangutils.utils.UtilConstants.INSTANCE_OF;
import static org.onosproject.yangutils.utils.UtilConstants.INT;
import static org.onosproject.yangutils.utils.UtilConstants.LIST;
import static org.onosproject.yangutils.utils.UtilConstants.NEW;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.OBJ;
import static org.onosproject.yangutils.utils.UtilConstants.OBJECT;
import static org.onosproject.yangutils.utils.UtilConstants.OBJECT_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.OF;
import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.OPEN_PARENTHESIS;
import static org.onosproject.yangutils.utils.UtilConstants.OTHER;
import static org.onosproject.yangutils.utils.UtilConstants.OVERRIDE;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC;
import static org.onosproject.yangutils.utils.UtilConstants.QUOTES;
import static org.onosproject.yangutils.utils.UtilConstants.RETURN;
import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
import static org.onosproject.yangutils.utils.UtilConstants.SET_METHOD_PREFIX;
import static org.onosproject.yangutils.utils.UtilConstants.SIXTEEN_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
import static org.onosproject.yangutils.utils.UtilConstants.STATIC;
import static org.onosproject.yangutils.utils.UtilConstants.STRING_DATA_TYPE;
import static org.onosproject.yangutils.utils.UtilConstants.SUFFIX_S;
import static org.onosproject.yangutils.utils.UtilConstants.THIS;
import static org.onosproject.yangutils.utils.UtilConstants.TO;
import static org.onosproject.yangutils.utils.UtilConstants.TRUE;
import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.VALUE;
import static org.onosproject.yangutils.utils.UtilConstants.VOID;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILD_METHOD;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.CONSTRUCTOR;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.DEFAULT_CONSTRUCTOR;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.SETTER_METHOD;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
/**
* Generated methods for generated files based on the file type.
......@@ -40,7 +101,7 @@ public final class MethodsGenerator {
*/
public static String parseBuilderInterfaceBuildMethodString(String name) {
return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.BUILD, name, false) + getBuildForInterface(name);
return getJavaDoc(BUILD_METHOD, name, false) + getBuildForInterface(name);
}
/**
......@@ -52,10 +113,10 @@ public final class MethodsGenerator {
public static String getGetterString(JavaAttributeInfo attr) {
String returnType = getReturnType(attr);
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
String attributeName = getLowerCase(attr.getAttributeName());
return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.GETTER, attributeName, attr.isListAttr()) +
getGetterForInterface(attributeName, returnType, attr.isListAttr());
return getJavaDoc(GETTER_METHOD, attributeName, attr.isListAttr())
+ getGetterForInterface(attributeName, returnType, attr.isListAttr());
}
/**
......@@ -68,9 +129,9 @@ public final class MethodsGenerator {
public static String getSetterString(JavaAttributeInfo attr, String className) {
String attrType = getReturnType(attr);
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
String attributeName = getLowerCase(attr.getAttributeName());
return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.SETTER, attributeName, attr.isListAttr())
return getJavaDoc(SETTER_METHOD, attributeName, attr.isListAttr())
+ getSetterForInterface(attributeName, attrType, className, attr.isListAttr());
}
......@@ -82,7 +143,7 @@ public final class MethodsGenerator {
*/
public static String getConstructorString(String name) {
return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.CONSTRUCTOR, name, false);
return getJavaDoc(CONSTRUCTOR, name, false);
}
/**
......@@ -94,8 +155,7 @@ public final class MethodsGenerator {
*/
public static String getDefaultConstructorString(String name, String modifierType) {
return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.DEFAULT_CONSTRUCTOR, name, false)
+ getDefaultConstructor(name, modifierType);
return getJavaDoc(DEFAULT_CONSTRUCTOR, name, false) + getDefaultConstructor(name, modifierType);
}
/**
......@@ -108,12 +168,12 @@ public final class MethodsGenerator {
public static String getTypeDefConstructor(JavaAttributeInfo attr, String className) {
String attrQuaifiedType = getReturnType(attr);
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
String attributeName = getLowerCase(attr.getAttributeName());
if (!attr.isListAttr()) {
return getTypeDefConstructorString(attrQuaifiedType, attributeName, className);
}
String listAttr = getListString() + attrQuaifiedType + UtilConstants.DIAMOND_CLOSE_BRACKET;
String listAttr = getListString() + attrQuaifiedType + DIAMOND_CLOSE_BRACKET;
return getTypeDefConstructorString(listAttr, attributeName, className);
}
......@@ -127,13 +187,10 @@ public final class MethodsGenerator {
*/
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 + UtilConstants.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
+ UtilConstants.VALUE + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
return FOUR_SPACE_INDENTATION + PUBLIC + SPACE + className + OPEN_PARENTHESIS + type + SPACE + VALUE
+ CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + EIGHT_SPACE_INDENTATION + THIS + PERIOD
+ name + SPACE + EQUAL + SPACE + VALUE + SEMI_COLAN + NEW_LINE + FOUR_SPACE_INDENTATION
+ CLOSE_CURLY_BRACKET;
}
/**
......@@ -144,9 +201,8 @@ public final class MethodsGenerator {
*/
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;
return EIGHT_SPACE_INDENTATION + CHECK_NOT_NULL_STRING + OPEN_PARENTHESIS + name + COMMA + SPACE + name
+ CLOSE_PARENTHESIS + SEMI_COLAN + NEW_LINE;
}
/**
......@@ -157,8 +213,7 @@ public final class MethodsGenerator {
*/
public static String getBuildString(String name) {
return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.OVERRIDE + UtilConstants.NEW_LINE
+ getBuild(name);
return FOUR_SPACE_INDENTATION + OVERRIDE + NEW_LINE + getBuild(name);
}
/**
......@@ -170,13 +225,13 @@ public final class MethodsGenerator {
public static String getGetterForClass(JavaAttributeInfo attr) {
String attrQuaifiedType = getReturnType(attr);
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
String attributeName = getLowerCase(attr.getAttributeName());
if (!attr.isListAttr()) {
return getGetter(attrQuaifiedType, attributeName);
}
String listAttr = getListString() + attrQuaifiedType + UtilConstants.DIAMOND_CLOSE_BRACKET;
return getGetter(listAttr, attributeName + UtilConstants.SUFIX_S);
String listAttr = getListString() + attrQuaifiedType + DIAMOND_CLOSE_BRACKET;
return getGetter(listAttr, attributeName);
}
/**
......@@ -188,13 +243,9 @@ public final class MethodsGenerator {
*/
private static String getGetter(String type, String name) {
return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
+ 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 + name + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
return FOUR_SPACE_INDENTATION + PUBLIC + SPACE + type + SPACE + GET_METHOD_PREFIX + getCaptialCase(name)
+ OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + EIGHT_SPACE_INDENTATION
+ RETURN + SPACE + name + SEMI_COLAN + NEW_LINE + FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET;
}
/**
......@@ -207,12 +258,12 @@ public final class MethodsGenerator {
public static String getSetterForClass(JavaAttributeInfo attr, String className) {
String attrQuaifiedType = getReturnType(attr);
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
String attributeName = getLowerCase(attr.getAttributeName());
if (!attr.isListAttr()) {
return getSetter(className, attributeName, attrQuaifiedType);
}
String listAttr = getListString() + attrQuaifiedType + UtilConstants.DIAMOND_CLOSE_BRACKET;
return getSetter(className, attributeName + UtilConstants.SUFIX_S, listAttr);
String listAttr = getListString() + attrQuaifiedType + DIAMOND_CLOSE_BRACKET;
return getSetter(className, attributeName, listAttr);
}
/**
......@@ -225,16 +276,11 @@ public final class MethodsGenerator {
*/
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(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
+ 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;
return FOUR_SPACE_INDENTATION + PUBLIC + SPACE + className + BUILDER + SPACE + SET_METHOD_PREFIX
+ getCaptialCase(name) + OPEN_PARENTHESIS + type + SPACE + name + CLOSE_PARENTHESIS + SPACE
+ OPEN_CURLY_BRACKET + NEW_LINE + EIGHT_SPACE_INDENTATION + THIS + PERIOD + name + SPACE + EQUAL + SPACE
+ name + SEMI_COLAN + NEW_LINE + EIGHT_SPACE_INDENTATION + RETURN + SPACE + THIS + SEMI_COLAN
+ NEW_LINE + FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET;
}
/**
......@@ -246,14 +292,9 @@ public final class MethodsGenerator {
public static String getSetterForTypeDefClass(JavaAttributeInfo attr) {
String attrQuaifiedType = getReturnType(attr);
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
if (!attr.isListAttr()) {
String attributeName = getLowerCase(attr.getAttributeName());
return getTypeDefSetter(attrQuaifiedType, attributeName);
}
String listAttr = getListString() + attrQuaifiedType + UtilConstants.DIAMOND_CLOSE_BRACKET;
return getTypeDefSetter(listAttr, attributeName + UtilConstants.SUFIX_S);
}
/**
* Returns type def's setter for attribute.
......@@ -264,15 +305,10 @@ public final class MethodsGenerator {
*/
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 + UtilConstants.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
+ UtilConstants.VALUE + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
return FOUR_SPACE_INDENTATION + PUBLIC + SPACE + VOID + SPACE + SET_METHOD_PREFIX + getCaptialCase(name)
+ OPEN_PARENTHESIS + type + SPACE + VALUE + CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE
+ EIGHT_SPACE_INDENTATION + THIS + PERIOD + name + SPACE + EQUAL + SPACE + VALUE + SEMI_COLAN + NEW_LINE
+ FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET;
}
/**
......@@ -282,8 +318,7 @@ public final class MethodsGenerator {
*/
public static String getOverRideString() {
return UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.OVERRIDE + UtilConstants.NEW_LINE;
return NEW_LINE + FOUR_SPACE_INDENTATION + OVERRIDE + NEW_LINE;
}
/**
......@@ -299,8 +334,8 @@ public final class MethodsGenerator {
if (!isList) {
return getGetterInterfaceString(returnType, yangName);
}
String listAttr = getListString() + returnType + UtilConstants.DIAMOND_CLOSE_BRACKET;
return getGetterInterfaceString(listAttr, yangName + UtilConstants.SUFIX_S);
String listAttr = getListString() + returnType + DIAMOND_CLOSE_BRACKET;
return getGetterInterfaceString(listAttr, yangName);
}
/**
......@@ -312,12 +347,8 @@ public final class MethodsGenerator {
*/
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;
return FOUR_SPACE_INDENTATION + returnType + SPACE + GET_METHOD_PREFIX + getCaptialCase(yangName)
+ OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SEMI_COLAN;
}
/**
......@@ -334,8 +365,8 @@ public final class MethodsGenerator {
if (!isList) {
return getSetterInterfaceString(className, attrName, attrType);
}
String listAttr = getListString() + attrType + UtilConstants.DIAMOND_CLOSE_BRACKET;
return getSetterInterfaceString(className, attrName + UtilConstants.SUFIX_S, listAttr);
String listAttr = getListString() + attrType + DIAMOND_CLOSE_BRACKET;
return getSetterInterfaceString(className, attrName, listAttr);
}
/**
......@@ -348,11 +379,8 @@ public final class MethodsGenerator {
*/
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
+ attrType + UtilConstants.SPACE + attrName + UtilConstants.CLOSE_PARENTHESIS
+ UtilConstants.SEMI_COLAN;
return FOUR_SPACE_INDENTATION + className + BUILDER + SPACE + SET_METHOD_PREFIX + getCaptialCase(attrName)
+ OPEN_PARENTHESIS + attrType + SPACE + attrName + CLOSE_PARENTHESIS + SEMI_COLAN;
}
/**
......@@ -362,7 +390,7 @@ public final class MethodsGenerator {
*/
private static String getListString() {
return UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET;
return LIST + DIAMOND_OPEN_BRACKET;
}
/**
......@@ -373,9 +401,9 @@ public final class MethodsGenerator {
*/
private static String getReturnType(JavaAttributeInfo attr) {
String returnType = UtilConstants.EMPTY_STRING;
String returnType = EMPTY_STRING;
if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
returnType = attr.getImportInfo().getPkgInfo() + UtilConstants.PERIOD;
returnType = attr.getImportInfo().getPkgInfo() + PERIOD;
}
returnType = returnType + attr.getImportInfo().getClassInfo();
return returnType;
......@@ -389,9 +417,8 @@ public final class MethodsGenerator {
*/
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.NEW_LINE;
return FOUR_SPACE_INDENTATION + yangName + SPACE + BUILD + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SEMI_COLAN
+ NEW_LINE;
}
/**
......@@ -402,12 +429,10 @@ public final class MethodsGenerator {
*/
public static String getConstructorStart(String yangName) {
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 + UtilConstants.BUILDER.toLowerCase() + UtilConstants.OBJECT
+ UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
+ UtilConstants.NEW_LINE;
String javadoc = getConstructorString(yangName);
String constructor = FOUR_SPACE_INDENTATION + PUBLIC + SPACE + yangName + IMPL + OPEN_PARENTHESIS + yangName
+ BUILDER + SPACE + BUILDER.toLowerCase() + OBJECT + CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET
+ NEW_LINE;
return javadoc + constructor;
}
......@@ -420,17 +445,12 @@ public final class MethodsGenerator {
*/
public static String getConstructor(String yangName, JavaAttributeInfo attr) {
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
if (attr.isListAttr()) {
attributeName = attributeName + UtilConstants.SUFIX_S;
}
String constructor = UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.THIS
+ UtilConstants.PERIOD + JavaIdentifierSyntax.getCamelCase(attributeName)
+ UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.SPACE + UtilConstants.BUILDER.toLowerCase()
+ 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;
String attributeName = getLowerCase(attr.getAttributeName());
String constructor = EIGHT_SPACE_INDENTATION + THIS + PERIOD + getCamelCase(attributeName) + SPACE + EQUAL
+ SPACE + BUILDER.toLowerCase() + OBJECT + PERIOD + GET_METHOD_PREFIX
+ getCaptialCase(getCamelCase(attributeName)) + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SEMI_COLAN
+ NEW_LINE;
return constructor;
}
......@@ -443,14 +463,10 @@ public final class MethodsGenerator {
*/
public static String getBuild(String yangName) {
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
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.THIS + UtilConstants.CLOSE_PARENTHESIS
+ UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.CLOSE_CURLY_BRACKET;
return FOUR_SPACE_INDENTATION + PUBLIC + SPACE + yangName + SPACE + BUILD + OPEN_PARENTHESIS + CLOSE_PARENTHESIS
+ SPACE + OPEN_CURLY_BRACKET + NEW_LINE + EIGHT_SPACE_INDENTATION + RETURN + SPACE + NEW + SPACE
+ yangName + IMPL + OPEN_PARENTHESIS + THIS + CLOSE_PARENTHESIS + SEMI_COLAN + NEW_LINE
+ FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET;
}
/**
......@@ -462,10 +478,8 @@ public final class MethodsGenerator {
*/
private static String getDefaultConstructor(String name, String modifierType) {
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;
return FOUR_SPACE_INDENTATION + modifierType + SPACE + name + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SPACE
+ OPEN_CURLY_BRACKET + NEW_LINE + FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET;
}
/**
......@@ -475,11 +489,9 @@ public final class MethodsGenerator {
*/
public static String getToStringMethodOpen() {
return getOverRideString() + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
+ UtilConstants.STRING + UtilConstants.SPACE + UtilConstants.TO + UtilConstants.STRING
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE
+ UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION
+ UtilConstants.RETURN + UtilConstants.GOOGLE_MORE_OBJECT_METHOD_STRING + UtilConstants.NEW_LINE;
return getOverRideString() + FOUR_SPACE_INDENTATION + PUBLIC + SPACE + STRING_DATA_TYPE + SPACE + TO
+ STRING_DATA_TYPE + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE
+ EIGHT_SPACE_INDENTATION + RETURN + GOOGLE_MORE_OBJECT_METHOD_STRING + NEW_LINE;
}
/**
......@@ -489,10 +501,8 @@ public final class MethodsGenerator {
*/
public static String getToStringMethodClose() {
return UtilConstants.TWELVE_SPACE_INDENTATION + UtilConstants.PERIOD + UtilConstants.TO + UtilConstants.STRING
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN
+ UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE;
return TWELVE_SPACE_INDENTATION + PERIOD + TO + STRING_DATA_TYPE + OPEN_PARENTHESIS + CLOSE_PARENTHESIS
+ SEMI_COLAN + NEW_LINE + FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET + NEW_LINE;
}
/**
......@@ -503,14 +513,10 @@ public final class MethodsGenerator {
*/
public static String getToStringMethod(JavaAttributeInfo attr) {
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
if (attr.isListAttr()) {
attributeName = attributeName + UtilConstants.SUFIX_S;
}
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;
String attributeName = getLowerCase(attr.getAttributeName());
return TWELVE_SPACE_INDENTATION + PERIOD + ADD_STRING + OPEN_PARENTHESIS + QUOTES + attributeName + QUOTES
+ COMMA + SPACE + attributeName + CLOSE_PARENTHESIS;
}
......@@ -521,12 +527,9 @@ public final class MethodsGenerator {
*/
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 + UtilConstants.SPACE + UtilConstants.OBJECT_STRING + UtilConstants.SUFIX_S
+ UtilConstants.PERIOD + UtilConstants.HASH + UtilConstants.OPEN_PARENTHESIS;
return getOverRideString() + FOUR_SPACE_INDENTATION + PUBLIC + SPACE + INT + SPACE + HASH_CODE_STRING
+ OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + EIGHT_SPACE_INDENTATION
+ RETURN + SPACE + OBJECT_STRING + SUFFIX_S + PERIOD + HASH + OPEN_PARENTHESIS;
}
/**
......@@ -537,10 +540,10 @@ public final class MethodsGenerator {
*/
public static String getHashCodeMethodClose(String hashcodeString) {
hashcodeString = YangIoUtils.trimAtLast(hashcodeString, UtilConstants.COMMA);
hashcodeString = YangIoUtils.trimAtLast(hashcodeString, UtilConstants.SPACE);
return hashcodeString + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE;
hashcodeString = trimAtLast(hashcodeString, COMMA);
hashcodeString = trimAtLast(hashcodeString, SPACE);
return hashcodeString + CLOSE_PARENTHESIS + SEMI_COLAN + NEW_LINE + FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET
+ NEW_LINE;
}
/**
......@@ -551,13 +554,7 @@ public final class MethodsGenerator {
*/
public static String getHashCodeMethod(JavaAttributeInfo attr) {
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
if (attr.isListAttr()) {
attributeName = attributeName + UtilConstants.SUFIX_S;
}
return attributeName
+ UtilConstants.COMMA + UtilConstants.SPACE;
return getLowerCase(attr.getAttributeName()) + COMMA + SPACE;
}
/**
......@@ -568,12 +565,9 @@ public final class MethodsGenerator {
*/
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 + UtilConstants.OBJ
+ UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
+ UtilConstants.NEW_LINE + getEqualsMethodsCommonIfCondition()
+ getEqualsMethodsSpecificIfCondition(className);
return getOverRideString() + FOUR_SPACE_INDENTATION + PUBLIC + SPACE + BOOLEAN_DATA_TYPE + SPACE + EQUALS_STRING
+ OPEN_PARENTHESIS + OBJECT_STRING + SPACE + OBJ + CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET
+ NEW_LINE + getEqualsMethodsCommonIfCondition() + getEqualsMethodsSpecificIfCondition(className);
}
/**
......@@ -583,13 +577,9 @@ public final class MethodsGenerator {
*/
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 + UtilConstants.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;
return EIGHT_SPACE_INDENTATION + IF + SPACE + OPEN_PARENTHESIS + THIS + SPACE + EQUAL + EQUAL + SPACE + OBJ
+ CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + TWELVE_SPACE_INDENTATION + RETURN + SPACE
+ TRUE + SEMI_COLAN + NEW_LINE + EIGHT_SPACE_INDENTATION + CLOSE_CURLY_BRACKET + NEW_LINE;
}
/**
......@@ -600,14 +590,10 @@ public final class MethodsGenerator {
*/
private static String getEqualsMethodsSpecificIfCondition(String className) {
return UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.IF + UtilConstants.SPACE
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.OBJ + UtilConstants.INSTANCE_OF + className
+ UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
+ UtilConstants.NEW_LINE + UtilConstants.TWELVE_SPACE_INDENTATION + className + UtilConstants.SPACE
+ UtilConstants.OTHER + UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.SPACE
+ UtilConstants.OPEN_PARENTHESIS + className + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE
+ UtilConstants.OBJ + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
+ UtilConstants.TWELVE_SPACE_INDENTATION + UtilConstants.RETURN + UtilConstants.NEW_LINE;
return EIGHT_SPACE_INDENTATION + IF + SPACE + OPEN_PARENTHESIS + OBJ + INSTANCE_OF + className
+ CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + TWELVE_SPACE_INDENTATION + className
+ SPACE + OTHER + SPACE + EQUAL + SPACE + OPEN_PARENTHESIS + className + CLOSE_PARENTHESIS + SPACE
+ OBJ + SEMI_COLAN + NEW_LINE + TWELVE_SPACE_INDENTATION + RETURN + NEW_LINE;
}
/**
......@@ -618,17 +604,13 @@ public final class MethodsGenerator {
*/
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 + UtilConstants.NEW_LINE;
equalMethodString = trimAtLast(equalMethodString, AND);
equalMethodString = trimAtLast(equalMethodString, AND);
equalMethodString = trimAtLast(equalMethodString, SPACE);
equalMethodString = trimAtLast(equalMethodString, NEW_LINE) + SEMI_COLAN + NEW_LINE;
return equalMethodString + EIGHT_SPACE_INDENTATION + CLOSE_CURLY_BRACKET + NEW_LINE + EIGHT_SPACE_INDENTATION
+ RETURN + SPACE + FALSE + SEMI_COLAN + NEW_LINE + FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET
+ NEW_LINE;
}
/**
......@@ -639,15 +621,11 @@ public final class MethodsGenerator {
*/
public static String getEqualsMethod(JavaAttributeInfo attr) {
String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
if (attr.isListAttr()) {
attributeName = attributeName + UtilConstants.SUFIX_S;
}
return UtilConstants.SIXTEEN_SPACE_INDENTATION + UtilConstants.SPACE + UtilConstants.OBJECT_STRING
+ UtilConstants.SUFIX_S + UtilConstants.PERIOD + UtilConstants.EQUALS_STRING
+ UtilConstants.OPEN_PARENTHESIS + attributeName + UtilConstants.COMMA + UtilConstants.SPACE
+ UtilConstants.OTHER + UtilConstants.PERIOD + attributeName + UtilConstants.CLOSE_PARENTHESIS
+ UtilConstants.SPACE + UtilConstants.AND + UtilConstants.AND;
String attributeName = getLowerCase(attr.getAttributeName());
return SIXTEEN_SPACE_INDENTATION + SPACE + OBJECT_STRING + SUFFIX_S + PERIOD + EQUALS_STRING + OPEN_PARENTHESIS
+ attributeName + COMMA + SPACE + OTHER + PERIOD + attributeName + CLOSE_PARENTHESIS + SPACE + AND
+ AND;
}
......@@ -662,14 +640,10 @@ public final class MethodsGenerator {
String attrQuaifiedType = getReturnType(attr);
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;
return FOUR_SPACE_INDENTATION + PUBLIC + SPACE + STATIC + SPACE + name + SPACE + OF + OPEN_PARENTHESIS
+ attrQuaifiedType + SPACE + VALUE + CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE
+ EIGHT_SPACE_INDENTATION + RETURN + SPACE + NEW + SPACE + name + OPEN_PARENTHESIS + VALUE
+ CLOSE_PARENTHESIS + SEMI_COLAN + NEW_LINE + FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET + NEW_LINE;
}
}
......
......@@ -83,7 +83,7 @@ public final class UtilConstants {
public static final String INSTANCE_OF = " instanceof ";
public static final String VALUE = "value";
public static final String SUFIX_S = "s";
public static final String SUFFIX_S = "s";
public static final String IF = "if";
public static final String FOR = "for";
......@@ -152,7 +152,7 @@ public final class UtilConstants {
/**
* String built in java type.
*/
public static final String STRING = "String";
public static final String STRING_DATA_TYPE = "String";
/**
* java.lang.* packages.
*/
......@@ -161,7 +161,7 @@ public final class UtilConstants {
/**
* boolean built in java type.
*/
public static final String BOOLEAN = "boolean";
public static final String BOOLEAN_DATA_TYPE = "boolean";
/**
* byte java built in type.
......@@ -229,8 +229,7 @@ public final class UtilConstants {
public static final String DOUBLE_WRAPPER = "Double";
/**
* List of keywords in java, this is used for checking if the input does not
* contain these keywords.
* List of keywords in java, this is used for checking if the input does not contain these keywords.
*/
public static final List JAVA_KEY_WORDS = Arrays.asList("abstract", "assert", "boolean", "break", "byte", "case",
"catch", "char", "class", "const", "continue", "default", "do", "double", "else", "extends", "false",
......
......@@ -24,12 +24,16 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
/**
* Provides the license header for the generated files.
*/
public final class CopyrightHeader {
private static final int EOF = -1;
private static final String COPYRIGHT_HEADER_FILE = "CopyrightHeader.txt";
private static final String TEMP_FILE = "temp.txt";
private static ClassLoader classLoader = CopyrightHeader.class.getClassLoader();
private static String copyrightHeader;
......@@ -47,6 +51,7 @@ public final class CopyrightHeader {
* @throws IOException when fails to parse copyright header
*/
public static String getCopyrightHeader() throws IOException {
if (copyrightHeader == null) {
parseCopyrightHeader();
}
......@@ -59,6 +64,7 @@ public final class CopyrightHeader {
* @param header copyright header
*/
private static void setCopyrightHeader(String header) {
copyrightHeader = header;
}
......@@ -69,11 +75,12 @@ public final class CopyrightHeader {
*/
public static void parseCopyrightHeader() throws IOException {
File temp = new File("temp.txt");
File temp = new File(TEMP_FILE);
try {
InputStream stream = classLoader.getResourceAsStream("CopyrightHeader.txt");
InputStream stream = classLoader.getResourceAsStream(COPYRIGHT_HEADER_FILE);
OutputStream out = new FileOutputStream(temp);
int index;
while ((index = stream.read()) != EOF) {
out.write(index);
......@@ -98,18 +105,20 @@ public final class CopyrightHeader {
*/
private static String getStringFileContent(File toAppend) throws IOException {
BufferedReader bufferReader = new BufferedReader(new FileReader(toAppend));
FileReader fileReader = new FileReader(toAppend);
BufferedReader bufferReader = new BufferedReader(fileReader);
try {
StringBuilder stringBuilder = new StringBuilder();
String line = bufferReader.readLine();
while (line != null) {
stringBuilder.append(line);
stringBuilder.append("\n");
stringBuilder.append(NEW_LINE);
line = bufferReader.readLine();
}
return stringBuilder.toString();
} finally {
fileReader.close();
bufferReader.close();
}
}
......
......@@ -23,7 +23,17 @@ import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.onosproject.yangutils.utils.UtilConstants.EIGHT_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.MULTIPLE_NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addPackageInfo;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.convertPathToPkg;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.convertPkgToPath;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.createDirectories;
/**
* Utility to handle file system operations.
......@@ -44,8 +54,8 @@ public final class FileSystemUtil {
*/
public static boolean doesPackageExist(String pkg) {
File pkgDir = new File(pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
File pkgWithFile = new File(pkgDir + File.separator + "package-info.java");
File pkgDir = new File(convertPkgToPath(pkg));
File pkgWithFile = new File(pkgDir + SLASH + "package-info.java");
if (pkgDir.exists() && pkgWithFile.isFile()) {
return true;
}
......@@ -63,8 +73,8 @@ public final class FileSystemUtil {
if (!doesPackageExist(pkg)) {
try {
File pack = YangIoUtils.createDirectories(pkg);
YangIoUtils.addPackageInfo(pack, pkgInfo, pkg.replace(UtilConstants.SLASH, UtilConstants.PERIOD));
File pack = createDirectories(pkg);
addPackageInfo(pack, pkgInfo, convertPathToPkg(pkg));
} catch (IOException e) {
throw new IOException("failed to create package-info file");
}
......@@ -83,9 +93,7 @@ public final class FileSystemUtil {
*/
public static void appendFileContents(File toAppend, File srcFile) throws IOException {
updateFileHandle(srcFile,
UtilConstants.NEW_LINE + readAppendFile(toAppend.toString(), UtilConstants.FOUR_SPACE_INDENTATION),
false);
updateFileHandle(srcFile, NEW_LINE + readAppendFile(toAppend.toString(), FOUR_SPACE_INDENTATION), false);
return;
}
......@@ -106,15 +114,14 @@ public final class FileSystemUtil {
String line = bufferReader.readLine();
while (line != null) {
if (line.equals(UtilConstants.SPACE) | line.equals(UtilConstants.EMPTY_STRING)
| line.equals(UtilConstants.EIGHT_SPACE_INDENTATION)
| line.equals(UtilConstants.MULTIPLE_NEW_LINE)) {
stringBuilder.append(UtilConstants.NEW_LINE);
} else if (line.equals(UtilConstants.FOUR_SPACE_INDENTATION)) {
stringBuilder.append(UtilConstants.EMPTY_STRING);
if (line.equals(SPACE) | line.equals(EMPTY_STRING) | line.equals(EIGHT_SPACE_INDENTATION)
| line.equals(MULTIPLE_NEW_LINE)) {
stringBuilder.append(NEW_LINE);
} else if (line.equals(FOUR_SPACE_INDENTATION)) {
stringBuilder.append(EMPTY_STRING);
} else {
stringBuilder.append(spaces + line);
stringBuilder.append(UtilConstants.NEW_LINE);
stringBuilder.append(NEW_LINE);
}
line = bufferReader.readLine();
}
......@@ -131,9 +138,8 @@ public final class FileSystemUtil {
* @param inputFile input file
* @param contentTobeAdded content to be appended to the file
* @param isClose when close of file is called.
* @throws IOException if the named file exists but is a directory rather
* than a regular file, does not exist but cannot be created, or
* cannot be opened for any other reason
* @throws IOException if the named file exists but is a directory rather than a regular file,
* does not exist but cannot be created, or cannot be opened for any other reason
*/
public static void updateFileHandle(File inputFile, String contentTobeAdded, boolean isClose) throws IOException {
......
......@@ -19,6 +19,36 @@ package org.onosproject.yangutils.utils.io.impl;
import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
import static org.onosproject.yangutils.utils.UtilConstants.BUILDER_CLASS_JAVA_DOC;
import static org.onosproject.yangutils.utils.UtilConstants.BUILDER_INTERFACE_JAVA_DOC;
import static org.onosproject.yangutils.utils.UtilConstants.BUILDER_OBJECT;
import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
import static org.onosproject.yangutils.utils.UtilConstants.IMPL_CLASS_JAVA_DOC;
import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE_JAVA_DOC;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_DOC_BUILD;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_DOC_BUILD_RETURN;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_DOC_CONSTRUCTOR;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_DOC_DEFAULT_CONSTRUCTOR;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_DOC_END_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_DOC_FIRST_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_DOC_GETTERS;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_DOC_OF;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_DOC_PARAM;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_DOC_RETURN;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_DOC_SETTERS;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_DOC_SETTERS_COMMON;
import static org.onosproject.yangutils.utils.UtilConstants.LIST;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE_ESTRIC;
import static org.onosproject.yangutils.utils.UtilConstants.OBJECT;
import static org.onosproject.yangutils.utils.UtilConstants.OF;
import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE_INFO_JAVADOC;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
import static org.onosproject.yangutils.utils.UtilConstants.VALUE;
/**
* Provides javadoc for the generated classes.
*/
......@@ -63,17 +93,17 @@ public final class JavaDocGen {
/**
* For getters.
*/
GETTER,
GETTER_METHOD,
/**
* For setters.
*/
SETTER,
SETTER_METHOD,
/**
* For type def's setters.
*/
TYPE_DEF_SETTER,
TYPE_DEF_SETTER_METHOD,
/**
* For type def's constructor.
......@@ -83,7 +113,7 @@ public final class JavaDocGen {
/**
* For of method.
*/
OF,
OF_METHOD,
/**
* For default constructor.
......@@ -98,7 +128,7 @@ public final class JavaDocGen {
/**
* For build.
*/
BUILD
BUILD_METHOD
}
/**
......@@ -110,8 +140,9 @@ public final class JavaDocGen {
* @return javadocs.
*/
public static String getJavaDoc(JavaDocType type, String name, boolean isList) {
name = JavaIdentifierSyntax.getLowerCase(JavaIdentifierSyntax.getCamelCase(name));
String javaDoc = "";
String javaDoc = UtilConstants.EMPTY_STRING;
if (type.equals(JavaDocType.IMPL_CLASS)) {
javaDoc = generateForImplClass(name);
} else if (type.equals(JavaDocType.BUILDER_CLASS)) {
......@@ -122,19 +153,19 @@ public final class JavaDocGen {
javaDoc = generateForBuilderInterface(name);
} else if (type.equals(JavaDocType.PACKAGE_INFO)) {
javaDoc = generateForPackage(name);
} else if (type.equals(JavaDocType.GETTER)) {
} else if (type.equals(JavaDocType.GETTER_METHOD)) {
javaDoc = generateForGetters(name, isList);
} else if (type.equals(JavaDocType.TYPE_DEF_SETTER)) {
} else if (type.equals(JavaDocType.TYPE_DEF_SETTER_METHOD)) {
javaDoc = generateForTypeDefSetter(name);
} else if (type.equals(JavaDocType.TYPE_DEF_CONSTRUCTOR)) {
javaDoc = generateForTypeDefConstructor(name);
} else if (type.equals(JavaDocType.SETTER)) {
} else if (type.equals(JavaDocType.SETTER_METHOD)) {
javaDoc = generateForSetters(name, isList);
} else if (type.equals(JavaDocType.OF)) {
} else if (type.equals(JavaDocType.OF_METHOD)) {
javaDoc = generateForOf(name);
} else if (type.equals(JavaDocType.DEFAULT_CONSTRUCTOR)) {
javaDoc = generateForDefaultConstructors();
} else if (type.equals(JavaDocType.BUILD)) {
} else if (type.equals(JavaDocType.BUILD_METHOD)) {
javaDoc = generateForBuild(name);
} else {
javaDoc = generateForConstructors(name);
......@@ -150,19 +181,18 @@ public final class JavaDocGen {
* @return javaDocs
*/
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;
String getter = NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION
+ JAVA_DOC_GETTERS + attribute + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ESTRIC
+ FOUR_SPACE_INDENTATION + JAVA_DOC_RETURN;
if (isList) {
String listAttribute = UtilConstants.LIST.toLowerCase() + UtilConstants.SPACE + UtilConstants.OF
+ UtilConstants.SPACE;
String listAttribute = LIST.toLowerCase() + SPACE + OF + SPACE;
getter = getter + listAttribute;
} else {
getter = getter + VALUE + SPACE + OF + SPACE;
}
getter = getter + attribute + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.JAVA_DOC_END_LINE;
getter = getter + attribute + NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_END_LINE;
return getter;
}
......@@ -174,23 +204,18 @@ public final class JavaDocGen {
* @return javaDocs
*/
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;
String setter = NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION
+ JAVA_DOC_SETTERS + attribute + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ESTRIC
+ FOUR_SPACE_INDENTATION + JAVA_DOC_PARAM + attribute + SPACE;
if (isList) {
setter = setter + attribute + UtilConstants.SUFIX_S + UtilConstants.SPACE;
String listAttribute = UtilConstants.LIST.toLowerCase() + UtilConstants.SPACE + UtilConstants.OF
+ UtilConstants.SPACE;
String listAttribute = LIST.toLowerCase() + SPACE + OF + SPACE;
setter = setter + listAttribute;
} else {
setter = setter + attribute + UtilConstants.SPACE;
setter = setter + VALUE + SPACE + OF + SPACE;
}
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;
setter = setter + attribute + NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_RETURN + BUILDER_OBJECT + attribute
+ NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_END_LINE;
return setter;
}
......@@ -201,14 +226,12 @@ public final class JavaDocGen {
* @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;
return NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_OF
+ attribute + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ESTRIC + FOUR_SPACE_INDENTATION
+ JAVA_DOC_PARAM + VALUE + SPACE + VALUE + SPACE + OF + SPACE + attribute + NEW_LINE
+ FOUR_SPACE_INDENTATION + JAVA_DOC_RETURN + OBJECT + SPACE + OF + SPACE + attribute + NEW_LINE
+ FOUR_SPACE_INDENTATION + JAVA_DOC_END_LINE;
}
/**
......@@ -218,12 +241,11 @@ public final class JavaDocGen {
* @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;
return (NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION
+ JAVA_DOC_SETTERS_COMMON + attribute + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ESTRIC
+ FOUR_SPACE_INDENTATION + JAVA_DOC_PARAM + VALUE + SPACE + VALUE + SPACE + OF + SPACE + attribute
+ NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_END_LINE);
}
/**
......@@ -233,12 +255,11 @@ public final class JavaDocGen {
* @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;
return (NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_CONSTRUCTOR
+ attribute + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ESTRIC + FOUR_SPACE_INDENTATION
+ JAVA_DOC_PARAM + VALUE + SPACE + VALUE + SPACE + OF + SPACE + attribute + NEW_LINE
+ FOUR_SPACE_INDENTATION + JAVA_DOC_END_LINE);
}
/**
......@@ -248,8 +269,8 @@ public final class JavaDocGen {
* @return javaDocs
*/
private static String generateForImplClass(String className) {
return UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.IMPL_CLASS_JAVA_DOC
+ className + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE;
return NEW_LINE + JAVA_DOC_FIRST_LINE + IMPL_CLASS_JAVA_DOC + className + PERIOD + NEW_LINE + JAVA_DOC_END_LINE;
}
/**
......@@ -259,8 +280,9 @@ public final class JavaDocGen {
* @return javaDocs
*/
private static String generateForBuilderClass(String className) {
return UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.BUILDER_CLASS_JAVA_DOC
+ className + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE;
return NEW_LINE + JAVA_DOC_FIRST_LINE + BUILDER_CLASS_JAVA_DOC + className + PERIOD + NEW_LINE
+ JAVA_DOC_END_LINE;
}
/**
......@@ -270,8 +292,9 @@ public final class JavaDocGen {
* @return javaDocs
*/
private static String generateForInterface(String interfaceName) {
return UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.INTERFACE_JAVA_DOC
+ interfaceName + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE;
return NEW_LINE + JAVA_DOC_FIRST_LINE + INTERFACE_JAVA_DOC + interfaceName + PERIOD + NEW_LINE
+ JAVA_DOC_END_LINE;
}
/**
......@@ -281,8 +304,9 @@ public final class JavaDocGen {
* @return javaDocs
*/
private static String generateForBuilderInterface(String builderforName) {
return UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.BUILDER_INTERFACE_JAVA_DOC + builderforName
+ UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE;
return JAVA_DOC_FIRST_LINE + BUILDER_INTERFACE_JAVA_DOC + builderforName + PERIOD + NEW_LINE
+ JAVA_DOC_END_LINE;
}
/**
......@@ -292,8 +316,8 @@ public final class JavaDocGen {
* @return javaDocs
*/
private static String generateForPackage(String packageName) {
return UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.PACKAGE_INFO_JAVADOC + packageName
+ UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE;
return JAVA_DOC_FIRST_LINE + PACKAGE_INFO_JAVADOC + packageName + PERIOD + NEW_LINE + JAVA_DOC_END_LINE;
}
/**
......@@ -302,9 +326,9 @@ public final class JavaDocGen {
* @return javaDocs
*/
private static String generateForDefaultConstructors() {
return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_DEFAULT_CONSTRUCTOR
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_END_LINE;
return FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_DEFAULT_CONSTRUCTOR
+ FOUR_SPACE_INDENTATION + JAVA_DOC_END_LINE;
}
/**
......@@ -314,14 +338,12 @@ public final class JavaDocGen {
* @return javaDocs
*/
private static String generateForConstructors(String className) {
return UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_CONSTRUCTOR + className
+ UtilConstants.IMPL + UtilConstants.PERIOD + UtilConstants.NEW_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.NEW_LINE_ESTRIC
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_PARAM
+ UtilConstants.BUILDER.toLowerCase() + UtilConstants.OBJECT + UtilConstants.SPACE
+ UtilConstants.BUILDER_OBJECT + UtilConstants.SPACE + className + UtilConstants.NEW_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_END_LINE;
return NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE
+ FOUR_SPACE_INDENTATION + JAVA_DOC_CONSTRUCTOR + className + IMPL + PERIOD + NEW_LINE
+ FOUR_SPACE_INDENTATION + NEW_LINE_ESTRIC + FOUR_SPACE_INDENTATION + JAVA_DOC_PARAM
+ BUILDER.toLowerCase() + OBJECT + SPACE + BUILDER_OBJECT + SPACE + className + NEW_LINE
+ FOUR_SPACE_INDENTATION + JAVA_DOC_END_LINE;
}
/**
......@@ -331,11 +353,10 @@ public final class JavaDocGen {
* @return javaDocs
*/
private static String generateForBuild(String buildName) {
return UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_BUILD + buildName + UtilConstants.PERIOD
+ UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.NEW_LINE_ESTRIC
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_RETURN
+ UtilConstants.JAVA_DOC_BUILD_RETURN + buildName + UtilConstants.PERIOD + UtilConstants.NEW_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_END_LINE;
return NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_BUILD
+ buildName + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ESTRIC + FOUR_SPACE_INDENTATION
+ JAVA_DOC_RETURN + JAVA_DOC_BUILD_RETURN + buildName + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION
+ JAVA_DOC_END_LINE;
}
}
......
......@@ -23,10 +23,13 @@ import java.util.List;
import java.util.Stack;
/**
* Provides the IO services for Yangutils-maven-Plugin.
* Provides utility for searching the files in a directory.
*/
public final class YangFileScanner {
private static final String JAVA_FILE_EXTENTION = ".java";
private static final String YANG_FILE_EXTENTION = ".yang";
/**
* Default constructor.
*/
......@@ -44,7 +47,7 @@ public final class YangFileScanner {
*/
public static List<String> getJavaFiles(String root) throws NullPointerException, IOException {
return getFiles(root, ".java");
return getFiles(root, JAVA_FILE_EXTENTION);
}
/**
......@@ -58,7 +61,7 @@ public final class YangFileScanner {
*/
public static List<String> getYangFiles(String root) throws NullPointerException, IOException {
return getFiles(root, ".yang");
return getFiles(root, YANG_FILE_EXTENTION);
}
/**
......@@ -68,8 +71,7 @@ public final class YangFileScanner {
* @param extension file extension
* @return list of required files
* @throws NullPointerException when no file is there
* @throws IOException when files get deleted while performing the
* operations
* @throws IOException when files get deleted while performing the operations
*/
public static List<String> getFiles(String root, String extension) throws NullPointerException, IOException {
......
......@@ -25,13 +25,27 @@ import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.maven.model.Resource;
import org.apache.maven.project.MavenProject;
import org.onosproject.yangutils.utils.UtilConstants;
import org.slf4j.Logger;
import org.sonatype.plexus.build.incremental.BuildContext;
import static org.apache.commons.io.FileUtils.deleteDirectory;
import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.ORG;
import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
import static org.onosproject.yangutils.utils.UtilConstants.TEMP;
import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES;
import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents;
import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO;
import static org.slf4j.LoggerFactory.getLogger;
/**
......@@ -40,8 +54,7 @@ import static org.slf4j.LoggerFactory.getLogger;
public final class YangIoUtils {
private static final Logger log = getLogger(YangIoUtils.class);
private static final String TARGET_RESOURCE_PATH = UtilConstants.SLASH + UtilConstants.TEMP + UtilConstants.SLASH
+ UtilConstants.YANG_RESOURCES + UtilConstants.SLASH;
private static final String TARGET_RESOURCE_PATH = SLASH + TEMP + SLASH + YANG_RESOURCES + SLASH;
/**
* Default constructor.
......@@ -72,22 +85,24 @@ public final class YangIoUtils {
*/
public static void addPackageInfo(File path, String classInfo, String pack) throws IOException {
if (pack.contains(UtilConstants.ORG)) {
String[] strArray = pack.split(UtilConstants.ORG);
pack = UtilConstants.ORG + strArray[1];
if (pack.contains(ORG)) {
String[] strArray = pack.split(ORG);
pack = ORG + strArray[1];
}
try {
File packageInfo = new File(path + File.separator + "package-info.java");
File packageInfo = new File(path + SLASH + "package-info.java");
packageInfo.createNewFile();
FileWriter fileWriter = null;
BufferedWriter bufferedWriter = null;
fileWriter = new FileWriter(packageInfo);
bufferedWriter = new BufferedWriter(fileWriter);
FileWriter fileWriter = new FileWriter(packageInfo);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(CopyrightHeader.getCopyrightHeader());
bufferedWriter.write(JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.PACKAGE_INFO, classInfo, false));
bufferedWriter.write(UtilConstants.PACKAGE + UtilConstants.SPACE + pack + UtilConstants.SEMI_COLAN);
bufferedWriter.write(JavaDocGen.getJavaDoc(PACKAGE_INFO, classInfo, false));
bufferedWriter.write(PACKAGE + SPACE + pack + SEMI_COLAN);
bufferedWriter.close();
fileWriter.close();
} catch (IOException e) {
throw new IOException("Exception occured while creating package info file.");
}
......@@ -97,15 +112,16 @@ public final class YangIoUtils {
* Cleans the generated directory if already exist in source folder.
*
* @param dir generated directory in previous build
* @throws IOException when failed to delete directory
*/
public static void clean(String dir) {
public static void clean(String dir) throws IOException {
File generatedDirectory = new File(dir);
if (generatedDirectory.exists()) {
try {
FileUtils.deleteDirectory(generatedDirectory);
deleteDirectory(generatedDirectory);
} catch (IOException e) {
log.info("Failed to delete the generated files in " + generatedDirectory + " directory");
throw new IOException("Failed to delete the generated files in " + generatedDirectory + " directory");
}
}
}
......@@ -147,18 +163,17 @@ public final class YangIoUtils {
*/
public static String partString(String partString) {
String[] strArray = partString.split(UtilConstants.COMMA);
String newString = "";
String[] strArray = partString.split(COMMA);
String newString = EMPTY_STRING;
for (int i = 0; i < strArray.length; i++) {
if (i % 4 != 0 || i == 0) {
newString = newString + strArray[i] + UtilConstants.COMMA;
newString = newString + strArray[i] + COMMA;
} else {
newString = newString + UtilConstants.NEW_LINE + UtilConstants.TWELVE_SPACE_INDENTATION
+ strArray[i]
+ UtilConstants.COMMA;
newString = newString + NEW_LINE + TWELVE_SPACE_INDENTATION
+ strArray[i] + COMMA;
}
}
return trimAtLast(newString, UtilConstants.COMMA);
return trimAtLast(newString, COMMA);
}
/**
......@@ -183,13 +198,13 @@ public final class YangIoUtils {
public static String getDirectory(String baseCodeGenPath, String pathOfJavaPkg) {
if (pathOfJavaPkg.charAt(pathOfJavaPkg.length() - 1) == File.separatorChar) {
pathOfJavaPkg = trimAtLast(pathOfJavaPkg, UtilConstants.SLASH);
pathOfJavaPkg = trimAtLast(pathOfJavaPkg, SLASH);
}
String[] strArray = pathOfJavaPkg.split(UtilConstants.SLASH);
if (strArray[0].equals(UtilConstants.EMPTY_STRING)) {
String[] strArray = pathOfJavaPkg.split(SLASH);
if (strArray[0].equals(EMPTY_STRING)) {
return pathOfJavaPkg;
} else {
return baseCodeGenPath + File.separator + pathOfJavaPkg;
return baseCodeGenPath + SLASH + pathOfJavaPkg;
}
}
......@@ -212,8 +227,7 @@ public final class YangIoUtils {
* @param yangFiles list of YANG files
* @param outputDir project's output directory
* @param project maven project
* @throws IOException when fails to copy files to destination resource
* directory
* @throws IOException when fails to copy files to destination resource directory
*/
public static void copyYangFilesToTarget(List<String> yangFiles, String outputDir, MavenProject project)
throws IOException {
......@@ -226,11 +240,11 @@ public final class YangIoUtils {
for (File file : files) {
Files.copy(file.toPath(),
new File(path + file.getName()).toPath(),
(new File(path + file.getName())).toPath(),
StandardCopyOption.REPLACE_EXISTING);
}
Resource rsc = new Resource();
rsc.setDirectory(outputDir + UtilConstants.SLASH + UtilConstants.TEMP + UtilConstants.SLASH);
rsc.setDirectory(outputDir + SLASH + TEMP + SLASH);
project.addResource(rsc);
}
......@@ -248,4 +262,58 @@ public final class YangIoUtils {
}
return files;
}
/**
* Merge the temp java files to main java files.
*
* @param appendFile temp file
* @param srcFile main file
* @throws IOException when fails to append contents
*/
public static void mergeJavaFiles(File appendFile, File srcFile) throws IOException {
try {
appendFileContents(appendFile, srcFile);
} catch (IOException e) {
throw new IOException("Failed to append " + appendFile + " in " + srcFile);
}
}
/**
* Insert data in the generated file.
*
* @param file file in which need to be inserted
* @param data data which need to be inserted
* @throws IOException when fails to insert into file
*/
public static void insertDataIntoJavaFile(File file, String data) throws IOException {
try {
updateFileHandle(file, data, false);
} catch (IOException e) {
throw new IOException("Failed to insert in " + file + "file");
}
}
/**
* Convert directory path in java package format.
*
* @param path directory path
* @return java package
*/
public static String convertPathToPkg(String path) {
return path.replace(SLASH, PERIOD);
}
/**
* Convert java package in directory path format.
*
* @param pkg java package
* @return directory path
*/
public static String convertPkgToPath(String pkg) {
return pkg.replace(PERIOD, SLASH);
}
}
......
......@@ -16,18 +16,36 @@
package org.onosproject.yangutils.translator.tojava.utils;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.junit.Test;
import org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType;
import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes;
import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getImportText;
import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination;
import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefStart;
import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getListAttribute;
import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_CLOSE_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_OPEN_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
import static org.onosproject.yangutils.utils.UtilConstants.LIST;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE;
import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC;
import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
import static org.onosproject.yangutils.utils.UtilConstants.STRING_DATA_TYPE;
/**
* Unit test cases for java code snippet generator.
......@@ -36,10 +54,8 @@ public class JavaCodeSnippetGenTest {
private static final String PKG_INFO = "org.onosproject.unittest";
private static final String CLASS_INFO = "JavaCodeSnippetGenTest";
private static final int FILE_GEN_TYPE = GeneratedJavaFileType.INTERFACE_MASK;
private static final GeneratedMethodTypes METHOD_GEN_TYPE = GeneratedMethodTypes.GETTER;
private static final int FILE_GEN_TYPE = INTERFACE_MASK;
private static final String YANG_NAME = "Test";
private static final String STRING = "String";
/**
* Unit test for private constructor.
......@@ -54,6 +70,7 @@ public class JavaCodeSnippetGenTest {
@Test
public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
InstantiationException, IllegalAccessException, InvocationTargetException {
Class<?>[] classesToConstruct = {JavaCodeSnippetGen.class };
for (Class<?> clazz : classesToConstruct) {
Constructor<?> constructor = clazz.getDeclaredConstructor();
......@@ -67,14 +84,14 @@ public class JavaCodeSnippetGenTest {
*/
@Test
public void testForImportText() {
JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo();
importInfo.setPkgInfo(PKG_INFO);
importInfo.setClassInfo(CLASS_INFO);
String imports = JavaCodeSnippetGen.getImportText(importInfo);
String imports = getImportText(importInfo);
assertThat(true, is(imports.equals(UtilConstants.IMPORT + PKG_INFO + UtilConstants.PERIOD + CLASS_INFO
+ UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE)));
assertThat(true, is(imports.equals(IMPORT + PKG_INFO + PERIOD + CLASS_INFO + SEMI_COLAN + NEW_LINE)));
}
/**
......@@ -82,11 +99,10 @@ public class JavaCodeSnippetGenTest {
*/
@Test
public void testForJavaClassDefStart() {
String classDef = JavaCodeSnippetGen.getJavaClassDefStart(FILE_GEN_TYPE, YANG_NAME);
assertThat(true,
is(classDef.equals(UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.INTERFACE
+ UtilConstants.SPACE + YANG_NAME + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
+ UtilConstants.NEW_LINE)));
String classDef = getJavaClassDefStart(FILE_GEN_TYPE, YANG_NAME);
assertThat(true, is(classDef
.equals(PUBLIC + SPACE + INTERFACE + SPACE + YANG_NAME + SPACE + OPEN_CURLY_BRACKET + NEW_LINE)));
}
......@@ -95,36 +111,20 @@ public class JavaCodeSnippetGenTest {
*/
@Test
public void testForListAttribute() {
String listAttribute = JavaCodeSnippetGen.getListAttribute(STRING);
assertThat(true, is(listAttribute.equals(UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET + STRING
+ UtilConstants.DIAMOND_CLOSE_BRACKET)));
}
/**
* Unit test case for java class interface definition close.
*/
@Test
public void testForJavaClassDefInterfaceClose() {
String interfaceDef = JavaCodeSnippetGen.getJavaClassDefClose();
assertThat(true, is(interfaceDef.equals(UtilConstants.CLOSE_CURLY_BRACKET)));
String listAttribute = getListAttribute(STRING_DATA_TYPE);
assertThat(true,
is(listAttribute.equals(LIST + DIAMOND_OPEN_BRACKET + STRING_DATA_TYPE + DIAMOND_CLOSE_BRACKET)));
}
/**
* Unit test case for java class builder class definition close.
* Unit test case for java class interface definition close.
*/
@Test
public void testForJavaClassDefBuilderClassClose() {
String builderClassDef = JavaCodeSnippetGen.getJavaClassDefClose();
assertThat(true, is(builderClassDef.equals(UtilConstants.CLOSE_CURLY_BRACKET)));
}
public void testForJavaClassDefClose() {
/**
* Unit test case for java class typedef definition close.
*/
@Test
public void testForJavaClassDefTypeDefClose() {
String typeDef = JavaCodeSnippetGen.getJavaClassDefClose();
assertThat(true, is(typeDef.equals(UtilConstants.CLOSE_CURLY_BRACKET)));
String interfaceDef = getJavaClassDefClose();
assertThat(true, is(interfaceDef.equals(CLOSE_CURLY_BRACKET)));
}
/**
......@@ -133,30 +133,22 @@ public class JavaCodeSnippetGenTest {
@Test
public void testForJavaAttributeInfo() {
String attributeWithoutTypePkg = JavaCodeSnippetGen.getJavaAttributeDefination(null, UtilConstants.STRING,
YANG_NAME, false);
assertThat(true,
is(attributeWithoutTypePkg.equals(UtilConstants.PRIVATE + UtilConstants.SPACE + UtilConstants.STRING
+ UtilConstants.SPACE + YANG_NAME + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE)));
String attributeWithTypePkg = JavaCodeSnippetGen.getJavaAttributeDefination(
UtilConstants.JAVA_LANG, UtilConstants.STRING, YANG_NAME, false);
assertThat(true, is(attributeWithTypePkg
.equals(UtilConstants.PRIVATE + UtilConstants.SPACE + UtilConstants.JAVA_LANG + UtilConstants.PERIOD
+ UtilConstants.STRING + UtilConstants.SPACE + YANG_NAME + UtilConstants.SEMI_COLAN
+ UtilConstants.NEW_LINE)));
String attributeWithListPkg = JavaCodeSnippetGen.getJavaAttributeDefination(
UtilConstants.JAVA_LANG, UtilConstants.STRING, YANG_NAME, true);
assertThat(true,
is(attributeWithListPkg.equals(UtilConstants.PRIVATE + UtilConstants.SPACE + UtilConstants.LIST
+ UtilConstants.DIAMOND_OPEN_BRACKET + UtilConstants.JAVA_LANG + UtilConstants.PERIOD
+ UtilConstants.STRING + UtilConstants.DIAMOND_CLOSE_BRACKET + UtilConstants.SPACE + YANG_NAME
+ UtilConstants.SUFIX_S + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE)));
String attributeWithListWithoutPkg = JavaCodeSnippetGen.getJavaAttributeDefination(null, UtilConstants.STRING,
YANG_NAME, true);
assertThat(true,
is(attributeWithListWithoutPkg.equals(UtilConstants.PRIVATE + UtilConstants.SPACE + UtilConstants.LIST
+ UtilConstants.DIAMOND_OPEN_BRACKET + UtilConstants.STRING
+ UtilConstants.DIAMOND_CLOSE_BRACKET + UtilConstants.SPACE + YANG_NAME + UtilConstants.SUFIX_S
+ UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE)));
String attributeWithoutTypePkg = getJavaAttributeDefination(null, STRING_DATA_TYPE, YANG_NAME, false);
assertThat(true, is(attributeWithoutTypePkg.equals(
PRIVATE + SPACE + STRING_DATA_TYPE + SPACE + YANG_NAME + SEMI_COLAN + NEW_LINE)));
String attributeWithTypePkg = getJavaAttributeDefination(JAVA_LANG, STRING_DATA_TYPE, YANG_NAME, false);
assertThat(true, is(attributeWithTypePkg.equals(PRIVATE + SPACE + JAVA_LANG + PERIOD
+ STRING_DATA_TYPE + SPACE + YANG_NAME + SEMI_COLAN + NEW_LINE)));
String attributeWithListPkg = getJavaAttributeDefination(JAVA_LANG, STRING_DATA_TYPE, YANG_NAME, true);
assertThat(true, is(attributeWithListPkg.equals(
PRIVATE + SPACE + LIST + DIAMOND_OPEN_BRACKET + JAVA_LANG + PERIOD + STRING_DATA_TYPE
+ DIAMOND_CLOSE_BRACKET + SPACE + YANG_NAME + SEMI_COLAN + NEW_LINE)));
String attributeWithListWithoutPkg = getJavaAttributeDefination(null, STRING_DATA_TYPE, YANG_NAME, true);
assertThat(true, is(attributeWithListWithoutPkg.equals(
PRIVATE + SPACE + LIST + DIAMOND_OPEN_BRACKET + STRING_DATA_TYPE + DIAMOND_CLOSE_BRACKET + SPACE
+ YANG_NAME + SEMI_COLAN + 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;
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.tojava.JavaAttributeInfo;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
/**
* Unit tests for generated methods from the file type.
*/
public final class MethodsGeneratorTest {
public static JavaAttributeInfo testAttr;
public static YangType<?> attrType = new YangType<>();
/**
* 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 = {
MethodsGenerator.class
};
for (Class<?> clazz : classesToConstruct) {
Constructor<?> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
assertNotNull(constructor.newInstance());
}
}
// /**
// * Unit test case for checking the parse builder and typedef constructor.
// */
// @Test
// public void getParseBuilderInterfaceMethodConstructorTest() {
//
// JavaQualifiedTypeInfo forSetter = new JavaQualifiedTypeInfo();
// 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 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(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(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() {
//
// JavaQualifiedTypeInfo forGetterSetter = new JavaQualifiedTypeInfo();
// 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() {
//
// JavaQualifiedTypeInfo forSetter = new JavaQualifiedTypeInfo();
// 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 stringTypeDef = MethodsGenerator.getTypeDefConstructor(testAttr, "Testname");
// assertThat(stringTypeDef.contains("(List<") && stringTypeDef.contains("Testname")
// && stringTypeDef.contains(UtilConstants.THIS), is(true));
// }
}
......@@ -21,9 +21,7 @@ import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.junit.Assert.assertNotNull;
......@@ -34,12 +32,12 @@ import static org.junit.Assert.assertTrue;
*/
public final class FileSystemUtilTest {
public static final String BASE_DIR_PKG = "target.UnitTestCase.";
public static final String PKG_INFO_CONTENT = "testGeneration6";
public static final String BASE_PKG = "target/UnitTestCase";
@Rule
public ExpectedException thrown = ExpectedException.none();
private static final String BASE_DIR_PKG = "target.UnitTestCase.";
private static final String PKG_INFO_CONTENT = "testGeneration6";
private static final String BASE_PKG = "target/UnitTestCase";
private static final String TEST_DATA_1 = "This is to append a text to the file first1\n";
private static final String TEST_DATA_2 = "This is next second line\n";
private static final String TEST_DATA_3 = "This is next third line in the file";
/**
* A private constructor is tested.
......@@ -47,20 +45,15 @@ public final class FileSystemUtilTest {
* @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 = {
FileSystemUtil.class
};
Class<?>[] classesToConstruct = {FileSystemUtil.class };
for (Class<?> clazz : classesToConstruct) {
Constructor<?> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
......@@ -70,6 +63,8 @@ public final class FileSystemUtilTest {
/**
* This test case checks the contents to be written in the file.
*
* @throws IOException when fails to create a test file
*/
@Test
public void updateFileHandleTest() throws IOException {
......@@ -80,15 +75,17 @@ public final class FileSystemUtilTest {
createFile.createNewFile();
File createSourceFile = new File(dir + "sourceTestFile");
createSourceFile.createNewFile();
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.updateFileHandle(createFile, TEST_DATA_1, false);
FileSystemUtil.updateFileHandle(createFile, TEST_DATA_2, false);
FileSystemUtil.updateFileHandle(createFile, TEST_DATA_3, false);
FileSystemUtil.appendFileContents(createFile, createSourceFile);
FileSystemUtil.updateFileHandle(createFile, null, true);
}
/**
* This test case checks whether the package is existing.
*
* @throws IOException when failed to create a test file
*/
@Test
public void packageExistTest() throws IOException {
......@@ -104,21 +101,4 @@ public final class FileSystemUtilTest {
createDir.delete();
}
/**
* This test case checks the package does not exist.
*/
// @Test
// public void packageNotExistTest() throws IOException {
// String dirPath = "notexist1.notexist2";
// String strPath = BASE_DIR_PKG + dirPath;
// File createDir = new File(strPath.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
// assertFalse(FileSystemUtil.doesPackageExist(strPath));
// createDir.mkdirs();
// assertFalse(FileSystemUtil.doesPackageExist(strPath));
// CopyrightHeader.parseCopyrightHeader();
// FileSystemUtil.createPackage(strPath, PKG_INFO_CONTENT);
// assertTrue(FileSystemUtil.doesPackageExist(strPath));
// createDir.delete();
// }
}
......
......@@ -62,7 +62,7 @@ public final class JavaDocGenTest {
@Test
public void buildGenerationTest() {
String buildDoc = JavaDocGen.getJavaDoc(JavaDocType.BUILD, "testGeneration1", false);
String buildDoc = JavaDocGen.getJavaDoc(JavaDocType.BUILD_METHOD, "testGeneration1", false);
assertTrue(buildDoc.contains("Builds object of") && buildDoc.contains(" */\n"));
}
......@@ -117,7 +117,7 @@ public final class JavaDocGenTest {
@Test
public void getterGenerationTest() {
String getterJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.GETTER, "testGeneration1", false);
String getterJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.GETTER_METHOD, "testGeneration1", false);
assertTrue(getterJavaDoc.contains("Returns the attribute") && getterJavaDoc.contains(" */\n"));
}
......@@ -157,7 +157,7 @@ public final class JavaDocGenTest {
@Test
public void setterGenerationTest() {
String setterJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.SETTER, "testGeneration1", false);
String setterJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.SETTER_METHOD, "testGeneration1", false);
assertTrue(setterJavaDoc.contains("Returns the builder object of") && setterJavaDoc.contains(" */\n"));
}
......@@ -167,7 +167,7 @@ public final class JavaDocGenTest {
@Test
public void typeDefSetterGenerationTest() {
String typeDefSetter = JavaDocGen.getJavaDoc(JavaDocType.TYPE_DEF_SETTER, "testGeneration1", false);
String typeDefSetter = JavaDocGen.getJavaDoc(JavaDocType.TYPE_DEF_SETTER_METHOD, "testGeneration1", false);
assertTrue(typeDefSetter.contains("Sets the value of") && typeDefSetter.contains(" */\n"));
}
}
\ No newline at end of file
......