KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > aval > support > validator > MatchesValidator


1 /**
2  * Spoon - http://spoon.gforge.inria.fr/
3  * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
4  *
5  * This software is governed by the CeCILL-C License under French law and
6  * abiding by the rules of distribution of free software. You can use,
7  * modify and/or redistribute the software under the terms of the
8  * CeCILL-C
9  * 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  */

20
21 package spoon.aval.support.validator;
22
23 import java.util.regex.Matcher JavaDoc;
24 import java.util.regex.Pattern JavaDoc;
25 import java.util.regex.PatternSyntaxException JavaDoc;
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 /**
36  * Implementation of the {@link Matches} value Validator
37  *
38  * <p>
39  * This class implements the {@link Matches} validator. It checks the
40  * value of the annotation against the provided regular expression. If the
41  * value does not match, the regular expression is not correct, or
42  * the &#64;Matches annotation is placed on an attribute of type
43  * different than <code>String</code> an ERROR is reported.
44  *
45  */

46 public class MatchesValidator implements Validator<Matches> {
47     /**
48      * Implementation of the Validator interface. Called by {@link AValProcessor}
49      */

50     public void check(ValidationPoint<Matches> vp) {
51         String JavaDoc regExp = vp.getValAnnotation().value();
52         String JavaDoc attribName = ((CtFieldReference)vp.getDslElement()).getSimpleName();
53         String JavaDoc target = (String JavaDoc)vp.getDslAnnotation().getElementValue(attribName);
54         if(target == null)return;
55         
56         try {
57             Pattern JavaDoc p = Pattern.compile(regExp);
58             Matcher JavaDoc m = p.matcher(target);
59             boolean matches = m.matches();
60             if(!matches){
61                 CtAnnotation dslAnnotation = vp.getDslAnnotation();
62                 String JavaDoc message = vp.getValAnnotation().message().replace("?value",target).replace("?regExp", regExp);
63                 ValidationPoint.report(Severity.ERROR, dslAnnotation, message);
64             }
65         } catch (PatternSyntaxException JavaDoc pe) {
66             CtElement dslElement = vp.getDslElement().getDeclaration();
67             if(dslElement == null) throw pe;
68             String JavaDoc 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