package strutsrewriting.node.creator; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.StringTokenizer; import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ArrayInitializer; import org.eclipse.jdt.core.dom.BooleanLiteral; import org.eclipse.jdt.core.dom.ImportDeclaration; import org.eclipse.jdt.core.dom.MemberValuePair; import org.eclipse.jdt.core.dom.NormalAnnotation; import org.eclipse.jdt.core.dom.QualifiedName; import org.eclipse.jdt.core.dom.SimpleName; import org.eclipse.jdt.core.dom.SimpleType; import org.eclipse.jdt.core.dom.StringLiteral; import org.eclipse.jdt.core.dom.TypeLiteral; import refactoring.struts.beans.ExceptionHandlingBean; import refactoring.struts.beans.ForwardAnnotationBean; import refactoring.struts.beans.MappingAnnotationBean; public class CreateAnnotations { private static final String FORWARD_ANNOTATION = "Forward"; private static final String FORWARDS_ANNOTATION = "Forwards"; private static final String MAPPING_ANNOTATION = "Mapping"; private static final String EXCEPTIONS_ANNOTATION = "Exceptions"; private static final String EXCEPTION_HANDLING_ANNOTATION = "ExceptionHandling"; private static final String TILE_ANNOTATION = "Tile"; public static List createAnnotationsForDA(AST ast, MappingAnnotationBean annotationBean) { List annotationsProduced = new ArrayList(); annotationsProduced.add(createMapping(ast, annotationBean)); if (!annotationBean.getForwardList().isEmpty()) { List forwardList = new ArrayList(); for (ForwardAnnotationBean forwardBean : annotationBean.getForwardList()) { forwardList.add(createForward(ast, forwardBean)); } annotationsProduced.add(createForwards(ast, forwardList)); } if (!annotationBean.getExceptionList().isEmpty()) { List exceptionList = new ArrayList(); for (ExceptionHandlingBean exceptionBean : annotationBean.getExceptionList()) { exceptionList.add(createExceptionHandling(ast, exceptionBean)); } annotationsProduced.add(createExceptions(ast, exceptionList)); } return annotationsProduced; } public static NormalAnnotation createForward(AST ast, ForwardAnnotationBean forwardBean) { NormalAnnotation forwardAnnotation = ast.newNormalAnnotation(); forwardAnnotation.setTypeName(forwardAnnotation.getAST().newName(FORWARD_ANNOTATION)); String name = forwardBean.getName(); String path = forwardBean.getPath(); boolean redirect = forwardBean.isRedirect(); boolean contextRelative = forwardBean.isContextRelative(); MemberValuePair keyValuePair; keyValuePair = createStringLiteral(ast, "name", name); forwardAnnotation.values().add(keyValuePair); keyValuePair = createStringLiteral(ast, "path", path); forwardAnnotation.values().add(keyValuePair); if(redirect) { keyValuePair = createBooleanLiteral(ast, "redirect", redirect); forwardAnnotation.values().add(keyValuePair); } if(contextRelative) { keyValuePair = createBooleanLiteral(ast, "contextRelative", contextRelative); forwardAnnotation.values().add(keyValuePair); } return forwardAnnotation; } public static NormalAnnotation createForwards(AST ast, List forwardList) { NormalAnnotation forwardsAnnotation = ast.newNormalAnnotation(); forwardsAnnotation.setTypeName(forwardsAnnotation.getAST().newName(FORWARDS_ANNOTATION)); MemberValuePair keyValuePair = createArray(ast, "value", forwardList); forwardsAnnotation.values().add(keyValuePair); return forwardsAnnotation; } public static NormalAnnotation createTile(AST ast, Map properties) { NormalAnnotation tileAnnotation = ast.newNormalAnnotation(); tileAnnotation.setTypeName(tileAnnotation.getAST().newName(TILE_ANNOTATION)); for (Map.Entry property : properties.entrySet()) { if (property.getKey().equals("body")) continue; MemberValuePair keyValuePair = createStringLiteral(ast, property.getKey().equals("body-context") ? "bodyContext" : property.getKey(), property.getValue()); tileAnnotation.values().add(keyValuePair); } return tileAnnotation; } public static NormalAnnotation createExceptionHandling(AST ast, ExceptionHandlingBean exceptionBean) { NormalAnnotation exceptionAnnotation = ast.newNormalAnnotation(); exceptionAnnotation.setTypeName(exceptionAnnotation.getAST().newName(EXCEPTION_HANDLING_ANNOTATION)); String exceptionTypeName = exceptionBean.getType(); String messageKey = exceptionBean.getKey(); String exceptionHandler = exceptionBean.getHandler(); String path = exceptionBean.getPath(); String scope = exceptionBean.getScope(); MemberValuePair keyValuePair; keyValuePair = createTypeLiteral(ast, "type", exceptionTypeName); exceptionAnnotation.values().add(keyValuePair); if (!isEmpty(messageKey)) { keyValuePair = createStringLiteral(ast, "key", messageKey); exceptionAnnotation.values().add(keyValuePair); } if (!isEmpty(exceptionHandler)) { keyValuePair = createTypeLiteral(ast, "handler", exceptionHandler); exceptionAnnotation.values().add(keyValuePair); } if (!isEmpty(path)) { keyValuePair = createStringLiteral(ast, "path", path); exceptionAnnotation.values().add(keyValuePair); } if (!isEmpty(scope)) { keyValuePair = createStringLiteral(ast, "scope", scope); exceptionAnnotation.values().add(keyValuePair); } return exceptionAnnotation; } public static NormalAnnotation createExceptions(AST ast, List exceptionList) { NormalAnnotation exceptionsAnnotation = ast.newNormalAnnotation(); exceptionsAnnotation.setTypeName(exceptionsAnnotation.getAST().newName(EXCEPTIONS_ANNOTATION)); MemberValuePair keyValuePair = createArray(ast, "value", exceptionList); exceptionsAnnotation.values().add(keyValuePair); return exceptionsAnnotation; } public static NormalAnnotation createMapping(AST ast, MappingAnnotationBean mappingBean) { NormalAnnotation mappingAnnotation = ast.newNormalAnnotation(); mappingAnnotation.setTypeName(mappingAnnotation.getAST().newName(MAPPING_ANNOTATION)); String path = mappingBean.getPath(); String formBean = mappingBean.getFormBean(); String attribute = mappingBean.getAttribute(); String formBeanClass = mappingBean.getFormBeanClass(); String input = mappingBean.getInput(); String module = mappingBean.getModule(); String scope = mappingBean.getScope(); boolean validate = mappingBean.isValidate(); String parameter = mappingBean.getParameter(); String customMappingClass = mappingBean.getCustomMappingClass(); String[] customMappingProperties = mappingBean.getCustomMappingProperties(); MemberValuePair keyValuePair; if (!isEmpty(module)) { module = (module.startsWith("/") ? module.substring(1) : module); keyValuePair = createStringLiteral(ast, "module", module); mappingAnnotation.values().add(keyValuePair); } if (!isEmpty(path)) { keyValuePair = createStringLiteral(ast, "path", path); mappingAnnotation.values().add(keyValuePair); } if (!isEmpty(input)) { keyValuePair = createStringLiteral(ast, "input", input); mappingAnnotation.values().add(keyValuePair); } if (!isEmpty(attribute)) { keyValuePair = createStringLiteral(ast, "attribute", attribute); mappingAnnotation.values().add(keyValuePair); } if (!isEmpty(formBean)) { keyValuePair = createStringLiteral(ast, "formBean", formBean); mappingAnnotation.values().add(keyValuePair); } if (!isEmpty(scope)) { keyValuePair = createStringLiteral(ast, "scope", scope); mappingAnnotation.values().add(keyValuePair); } if (!validate) { keyValuePair = createBooleanLiteral(ast, "validate", validate); mappingAnnotation.values().add(keyValuePair); } if (!isEmpty(parameter)) { keyValuePair = createStringLiteral(ast, "parameter", parameter); mappingAnnotation.values().add(keyValuePair); } return mappingAnnotation; } public static MemberValuePair createTypeLiteral(AST ast, String key, String value) { MemberValuePair pair = ast.newMemberValuePair(); pair.setName(pair.getAST().newSimpleName(key)); StringTokenizer tokens = new StringTokenizer(value.replace('$', '.'), "."); SimpleName topName = pair.getAST().newSimpleName(tokens.nextToken()); SimpleName curName = pair.getAST().newSimpleName(tokens.nextToken()); QualifiedName finalQualifiedName = pair.getAST().newQualifiedName(topName, curName); while (tokens.hasMoreTokens()) { curName = pair.getAST().newSimpleName(tokens.nextToken()); finalQualifiedName = pair.getAST().newQualifiedName(finalQualifiedName, curName); } SimpleType simpleType = pair.getAST().newSimpleType(finalQualifiedName); TypeLiteral literal = pair.getAST().newTypeLiteral(); literal.setType(simpleType); pair.setValue(literal); return pair; } public static MemberValuePair createStringLiteral(AST ast, String key, String value) { MemberValuePair pair = ast.newMemberValuePair(); pair.setName(pair.getAST().newSimpleName(key)); StringLiteral literal = pair.getAST().newStringLiteral(); literal.setLiteralValue(value); pair.setValue(literal); return pair; } public static MemberValuePair createBooleanLiteral(AST ast, String key, boolean value) { MemberValuePair pair = ast.newMemberValuePair(); pair.setName(pair.getAST().newSimpleName(key)); BooleanLiteral literal = pair.getAST().newBooleanLiteral(value); pair.setValue(literal); return pair; } public static MemberValuePair createArray(AST ast, String key, List values) { MemberValuePair pair = ast.newMemberValuePair(); pair.setName(pair.getAST().newSimpleName(key)); ArrayInitializer arrayInit = pair.getAST().newArrayInitializer(); for (Object value : values) { arrayInit.expressions().add(value); } pair.setValue(arrayInit); return pair; } private static boolean isEmpty(String str) { if (str == null) { return true; } return str.isEmpty(); } private static final String annotationsPackage = "pt.ist.fenixWebFramework.struts.annotations."; public static List createImportsForAnnotations(AST ast) { List importsForAnnotations = new ArrayList(); importsForAnnotations.add(createSingleImport(ast, annotationsPackage + "ExceptionHandling")); importsForAnnotations.add(createSingleImport(ast, annotationsPackage + "Exceptions")); importsForAnnotations.add(createSingleImport(ast, annotationsPackage + "Forward")); importsForAnnotations.add(createSingleImport(ast, annotationsPackage + "Forwards")); importsForAnnotations.add(createSingleImport(ast, annotationsPackage + "Mapping")); importsForAnnotations.add(createSingleImport(ast, annotationsPackage + "Tile")); return importsForAnnotations; } public static ImportDeclaration createSingleImport(AST ast, String fullQualifiedImport) { ImportDeclaration importDecl = ast.newImportDeclaration(); StringTokenizer tokens = new StringTokenizer(fullQualifiedImport, "."); SimpleName topName = ast.newSimpleName(tokens.nextToken()); SimpleName curName = ast.newSimpleName(tokens.nextToken()); QualifiedName finalQualifiedName = ast.newQualifiedName(topName, curName); while (tokens.hasMoreTokens()) { curName = ast.newSimpleName(tokens.nextToken()); finalQualifiedName = ast.newQualifiedName(finalQualifiedName, curName); } importDecl.setName(finalQualifiedName); return importDecl; } public static void changeForwardPathTiles(NormalAnnotation forwardAnn, AST ast, Map properties) { if (properties.size() > 1) { NormalAnnotation tileAnn = createTile(ast, properties); MemberValuePair pair = ast.newMemberValuePair(); pair.setName(pair.getAST().newSimpleName("tileProperties")); pair.setValue(tileAnn); forwardAnn.values().add(pair); } } }