01: /**
02: * Spoon - http://spoon.gforge.inria.fr/
03: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
04: *
05: * This software is governed by the CeCILL-C License under French law and
06: * abiding by the rules of distribution of free software. You can use,
07: * modify and/or redistribute the software under the terms of the
08: * CeCILL-C
09: * license as circulated by CEA, CNRS and INRIA at the following URL:
10: * http://www.cecill.info.
11: *
12: * This program is distributed in the hope that it will be useful, but
13: * WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C
15: * License for more details.
16: *
17: * The fact that you are presently reading this means that you have had
18: * knowledge of the CeCILL-C license and that you accept its terms.
19: */package spoon.aval.support.validator;
20:
21: import java.util.regex.Matcher;
22: import java.util.regex.Pattern;
23: import java.util.regex.PatternSyntaxException;
24:
25: import spoon.aval.Validator;
26: import spoon.aval.annotation.value.Matches;
27: import spoon.aval.processing.AValProcessor;
28: import spoon.aval.processing.ValidationPoint;
29: import spoon.processing.Severity;
30: import spoon.reflect.declaration.CtAnnotation;
31: import spoon.reflect.declaration.CtElement;
32: import spoon.reflect.reference.CtFieldReference;
33:
34: /**
35: * Implementation of the {@link Matches} value Validator
36: *
37: * <p>
38: * This class implements the {@link Matches} validator. It checks the
39: * value of the annotation against the provided regular expression. If the
40: * value does not match, the regular expression is not correct, or
41: * the @Matches annotation is placed on an attribute of type
42: * different than <code>String</code> an ERROR is reported.
43: *
44: */
45: public class MatchesValidator implements Validator<Matches> {
46: /**
47: * Implementation of the Validator interface. Called by {@link AValProcessor}
48: */
49: public void check(ValidationPoint<Matches> vp) {
50: String regExp = vp.getValAnnotation().value();
51: String attribName = ((CtFieldReference) vp.getDslElement())
52: .getSimpleName();
53: String target = (String) vp.getDslAnnotation().getElementValue(
54: attribName);
55: if (target == null)
56: return;
57:
58: try {
59: Pattern p = Pattern.compile(regExp);
60: Matcher m = p.matcher(target);
61: boolean matches = m.matches();
62: if (!matches) {
63: CtAnnotation dslAnnotation = vp.getDslAnnotation();
64: String message = vp.getValAnnotation().message()
65: .replace("?value", target).replace("?regExp",
66: regExp);
67: ValidationPoint.report(Severity.ERROR, dslAnnotation,
68: message);
69: }
70: } catch (PatternSyntaxException pe) {
71: CtElement dslElement = vp.getDslElement().getDeclaration();
72: if (dslElement == null)
73: throw pe;
74: String message = "The expression \'" + regExp
75: + "\' is not a valid Java Regular expression!";
76: ValidationPoint.report(vp.getValAnnotation().severity(),
77: dslElement, message, vp.fixerFactory(vp
78: .getValAnnotation().fixers()));
79: }
80: }
81:
82: }
|