1 20 21 package spoon.aval.support.validator; 22 23 import java.net.MalformedURLException ; 24 import java.net.URL ; 25 26 import spoon.aval.Validator; 27 import spoon.aval.annotation.value.URLValue; 28 import spoon.aval.processing.AValProcessor; 29 import spoon.aval.processing.ValidationPoint; 30 import spoon.processing.Severity; 31 import spoon.reflect.code.CtLiteral; 32 import spoon.reflect.declaration.CtAnnotation; 33 import spoon.reflect.declaration.CtField; 34 import spoon.reflect.reference.CtFieldReference; 35 36 46 public class URLValidator implements Validator<URLValue> { 47 51 @SuppressWarnings ("unchecked") 52 public void check(ValidationPoint<URLValue> vp) { 53 CtFieldReference dslElement = (CtFieldReference) vp.getDslElement(); 54 55 String attribName = (dslElement).getSimpleName(); 56 String value = (String ) vp.getDslAnnotation().getElementValue( 57 attribName); 58 CtField<?> dslfield = dslElement.getDeclaration(); 59 if(dslfield !=null && dslfield.getDefaultExpression() instanceof CtLiteral) { 60 CtLiteral defaultVal = (CtLiteral) dslfield.getDefaultExpression(); 61 if(value.equals(defaultVal.getValue())){ 62 return; 63 } 64 65 } 66 67 try { 68 new URL (value); 69 } catch (MalformedURLException ex) { 70 CtAnnotation dslAnnotation = vp.getDslAnnotation(); 71 String message = vp.getValAnnotation().message().replace("?val", value); 72 ValidationPoint.report(Severity.ERROR, dslAnnotation, message,vp.fixerFactory(vp.getValAnnotation().fixers())); 73 } 74 75 } 76 77 } 78 | Popular Tags |