1 20 21 package spoon.aval.support.validator; 22 23 import java.util.regex.Matcher ; 24 import java.util.regex.Pattern ; 25 import java.util.regex.PatternSyntaxException ; 26 27 import spoon.aval.Validator; 28 import spoon.aval.annotation.value.Matches; 29 import spoon.aval.processing.AValProcessor; 30 import spoon.aval.processing.ValidationPoint; 31 import spoon.processing.Severity; 32 import spoon.reflect.declaration.CtAnnotation; 33 import spoon.reflect.declaration.CtElement; 34 import spoon.reflect.reference.CtFieldReference; 35 46 public class MatchesValidator implements Validator<Matches> { 47 50 public void check(ValidationPoint<Matches> vp) { 51 String regExp = vp.getValAnnotation().value(); 52 String attribName = ((CtFieldReference)vp.getDslElement()).getSimpleName(); 53 String target = (String )vp.getDslAnnotation().getElementValue(attribName); 54 if(target == null)return; 55 56 try { 57 Pattern p = Pattern.compile(regExp); 58 Matcher m = p.matcher(target); 59 boolean matches = m.matches(); 60 if(!matches){ 61 CtAnnotation dslAnnotation = vp.getDslAnnotation(); 62 String message = vp.getValAnnotation().message().replace("?value",target).replace("?regExp", regExp); 63 ValidationPoint.report(Severity.ERROR, dslAnnotation, message); 64 } 65 } catch (PatternSyntaxException pe) { 66 CtElement dslElement = vp.getDslElement().getDeclaration(); 67 if(dslElement == null) throw pe; 68 String message = "The expression \'"+regExp+"\' is not a valid Java Regular expression!"; 69 ValidationPoint.report(vp.getValAnnotation().severity(), dslElement, message,vp.fixerFactory(vp.getValAnnotation().fixers())); 70 } 71 } 72 73 } 74 | Popular Tags |