package refactoring.struts; import java.util.StringTokenizer; import refactoring.struts.beans.SyntheticDispatchAction; public class DispatchActionCreator { public static SyntheticDispatchAction createDispatchAction(String moduleName, String dispatchActionQualifiedName) { StringBuilder sourceCodeDA = new StringBuilder(); sourceCodeDA.append(dispatchActionPackage(getModuleNameForQualifiedClassName(moduleName), dispatchActionQualifiedName)); sourceCodeDA.append("\n"); sourceCodeDA.append(classSignature(moduleName, dispatchActionQualifiedName)); return new SyntheticDispatchAction(sourceCodeDA.toString(), qualifiedClassName(moduleName, dispatchActionQualifiedName)); } private static String dispatchActionPackage(String moduleAdapted, String dispatchActionQualifiedName) { StringBuilder packageName = new StringBuilder(); String packageWithoutClass = dispatchActionQualifiedName.substring(0, dispatchActionQualifiedName.lastIndexOf('.')); packageName.append("package " + packageWithoutClass + "." + moduleAdapted + ";" + "\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 qualifiedClassName(String moduleName, String dispatchActionQualifiedName) { StringBuilder qualifiedName = new StringBuilder(); String classNameToExtend = dispatchActionQualifiedName.substring(dispatchActionQualifiedName.lastIndexOf('.') + 1); String packageWithoutClass = dispatchActionQualifiedName.substring(0, dispatchActionQualifiedName.lastIndexOf('.')); qualifiedName.append(packageWithoutClass + "." + getModuleNameForQualifiedClassName(moduleName) + "." + classNameToExtend + "For" + getModuleNameForClassName(moduleName)); return qualifiedName.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(); } private static String getModuleNameForQualifiedClassName(String moduleName) { String name = moduleName; if (moduleName.startsWith("/")) { name = name.substring(1); } return name.replace('/', '.'); } }