package refactoring.struts; import java.util.StringTokenizer; public class DispatchActionCreator { public static void createDispatchAction(String moduleName, String dispatchActionQualifiedName) { StringBuilder sourceCodeDA = new StringBuilder(); sourceCodeDA.append(dispatchActionPackage(dispatchActionQualifiedName)); sourceCodeDA.append("\n"); sourceCodeDA.append(classSignature(moduleName, dispatchActionQualifiedName)); /* * TODO fill in the Mapping, Forwards and Forward (and Tile...) */ // System.out.println(sourceCodeDA.toString()); } private static String dispatchActionPackage(String dispatchActionQualifiedName) { StringBuilder packageName = new StringBuilder(); String packageWithoutClass = dispatchActionQualifiedName.substring(0, dispatchActionQualifiedName.lastIndexOf('.')); packageName.append("package " + packageWithoutClass + ";" + "\n"); return packageName.toString(); } private static String classSignature(String moduleName, String dispatchActionQualifiedName) { StringBuilder classSignature = new StringBuilder(); String classNameToExtend = dispatchActionQualifiedName.substring(dispatchActionQualifiedName.lastIndexOf('.') + 1); classSignature.append("public class " + classNameToExtend + "For" + getModuleNameForClassName(moduleName) + " extends " + dispatchActionQualifiedName + " {"); classSignature.append("\n"); classSignature.append("}"); return classSignature.toString(); } private static String getModuleNameForClassName(String moduleName) { StringTokenizer tokenizer = new StringTokenizer(moduleName, "/"); StringBuilder result = new StringBuilder(); while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); token = ("" + token.charAt(0)).toUpperCase() + token.substring(1); result.append(token); } return result.toString(); } }