Show ActionFixtureSource.java syntax highlighted
package ca.ucalgary.cpsc.ebe.fitClipse.FixtureGenerater;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.StringTokenizer;
import ca.ucalgary.cpsc.ebe.fitClipse.FixtureGenerater.JavaClass;
import ca.ucalgary.cpsc.ebe.fitClipse.FixtureGenerater.JavaFile;
import ca.ucalgary.cpsc.ebe.fitClipse.FixtureGenerater.JavaMethod;
import ca.ucalgary.cpsc.ebe.fitClipse.FixtureGenerater.JavaMethodContentNode;
import ca.ucalgary.cpsc.ebe.fitClipse.FixtureGenerater.JavaProperty;
import ca.ucalgary.cpsc.ebe.fitClipse.FixtureGenerater.JavaSourceUtil;
public class ActionFixtureSource extends FixtureSource{
private static final String TABLE_FORMAT_REGEX = "\\|(.*)\\|(.*)\\|";
private static String FIXTURE_CLASS_PACKAGE_REGEX = "\\|(!-)?(([a-zA-Z_]+[\\.])*)([a-zA-Z_]+)(-!)?\\|";
private String className;
private String packageName;
private HashMap methods = new HashMap(); //use the method name as a key and the value defines a variable (if required)
private LinkedList sourceFiles = new LinkedList();
private JavaFile current = null;
private StringBuffer messages = new StringBuffer();
public String generateSource(String wikiData, String sourcePath) throws Exception {
try{
doRows(wikiData);
messages.append("Creating java files...\n");
this.composeJavaSource(sourcePath);
messages.append("Number of files created: " + this.sourceFiles.size());
return messages.toString();
}catch(Exception e){
return e.getMessage();
}
}
private void doRows(String tableData) throws Exception{
String[] rows = tableData.split("\n");
for(int i = 1; i < rows.length; i++){
if(!(doStart(rows[i]) || doCheck(rows[i]) || doPress(rows[i]) || doEnter(rows[i]))){
throw new Exception("Invalid Action on row: " + rows[i]);
}
}
}
private void composeJavaSource(String sourcePath) throws Exception{
for(int i = 0; i < this.sourceFiles.size(); i++){
messages.append(((JavaFile)this.sourceFiles.get(i)).commitToFile(sourcePath));
messages.append("\n");
}
}
private boolean doCheck(String row) throws Exception{
StringTokenizer rowParts = new StringTokenizer(row, "|");
if("check".equals(removeWikiFormatting(rowParts.nextToken()))){
String name = removeWikiFormatting(rowParts.nextToken());
name = toCamelCase(name);
JavaMethod meth = new JavaMethod(name, "public");
meth.setReturnType(JavaSourceUtil.TYPE_STRING);
meth.addContentNode(new JavaMethodContentNode("\nreturn null;"));
JavaClass currentClass = this.current.getMainClass();
if(!currentClass.containsMethod(meth)){
//add it to the class
currentClass.addChildElement(meth);
}
return true;
}
return false;
}
private boolean doPress(String row) throws Exception{
StringTokenizer rowParts = new StringTokenizer(row, "|");
if("press".equals(removeWikiFormatting(rowParts.nextToken()))){
String name = removeWikiFormatting(rowParts.nextToken());
name = toCamelCase(name);
JavaMethod meth = new JavaMethod(name, "public");
meth.setReturnType(JavaSourceUtil.TYPE_VOID);
//meth.addContentNode(new JavaMethodContentNode("\nreturn null;"));
JavaClass currentClass = this.current.getMainClass();
if(!currentClass.containsMethod(meth)){
//add it to the class
currentClass.addChildElement(meth);
}
return true;
}
return false;
}
private boolean doEnter(String row) throws Exception{
StringTokenizer rowParts = new StringTokenizer(row, "|");
if("enter".equals(removeWikiFormatting(rowParts.nextToken()))){
String name = removeWikiFormatting(rowParts.nextToken());
name = toCamelCase(name);
JavaMethod method= new JavaMethod(name, "public");
method.setReturnType(JavaSourceUtil.TYPE_VOID);
method.addParameter(name, JavaSourceUtil.TYPE_STRING);
method.addContentNode(new JavaMethodContentNode("this." + name + " = " + name + ";"));
JavaProperty prop = new JavaProperty(name, "private");
prop.setType(JavaSourceUtil.TYPE_STRING);
JavaClass currentClass = this.current.getMainClass();
if(!currentClass.containsMethod(method)){
//add it to the class
currentClass.addChildElement(method);
}
if(!currentClass.containsProperty(prop)){
//add it to the class
currentClass.addChildElement(prop);
}
return true;
}
return false;
}
private boolean doStart(String row) throws Exception{
StringTokenizer rowParts = new StringTokenizer(row, "|");
if("start".equals(removeWikiFormatting(rowParts.nextToken()))){
//check if we have encountered this class yet
String[] qName = parseJavaQName(removeWikiFormatting(rowParts.nextToken()));
boolean found = false;
for(int i = 0; i < this.sourceFiles.size(); i++){
if((((JavaFile)this.sourceFiles.get(i)).getPackageName().equals(qName[0])) && ((JavaFile)this.sourceFiles.get(i)).getMainClass().getName().equals(qName[1])){
this.current = ((JavaFile)this.sourceFiles.get(i));
found = true;
break;
}
}
if(found){
}
if(!found){
this.current = new JavaFile();
this.current.addImport("fit.ActionFixture");
this.sourceFiles.add(current);
this.current.setPackageName(qName[0]);
JavaClass main =new JavaClass(qName[1], "public");
main.addSuperClass("ActionFixture");
this.current.setMainClass(main);
}
// this.current.setPackageName(qName[0]);
// JavaClass main =new JavaClass(qName[1], "public");
// main.addSuperClass("ActionFixture");
// this.current.setMainClass(main);
return true;
}
return false;
}
}
See more files for this project here