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.net.MalformedURLException;
22: import java.net.URL;
23:
24: import spoon.aval.Validator;
25: import spoon.aval.annotation.value.URLValue;
26: import spoon.aval.processing.AValProcessor;
27: import spoon.aval.processing.ValidationPoint;
28: import spoon.processing.Severity;
29: import spoon.reflect.declaration.CtAnnotation;
30: import spoon.reflect.reference.CtFieldReference;
31:
32: /**
33: * Implementation of the {@link URLValue} validator.
34: * <p>
35: *
36: * This class implements the {@link URLValue} validator. It checks that the
37: * current validation point is a valid URL by attempting to construct a
38: * {@link URL} object. If this throws a {@link MalformedURLException}, an ERROR
39: * is reported.
40: *
41: */
42: public class URLValidator implements Validator<URLValue> {
43: /**
44: * Implementation of the Validator interface. Called by
45: * {@link AValProcessor}
46: */
47: @SuppressWarnings("unchecked")
48: public void check(ValidationPoint<URLValue> vp) {
49: CtFieldReference dslElement = (CtFieldReference) vp
50: .getDslElement();
51:
52: String attribName = (dslElement).getSimpleName();
53: String value = (String) vp.getDslAnnotation().getElementValue(
54: attribName);
55:
56: try {
57: new URL(value);
58: } catch (MalformedURLException ex) {
59: CtAnnotation dslAnnotation = vp.getDslAnnotation();
60: String message = vp.getValAnnotation().message().replace(
61: "?val", value);
62: ValidationPoint.report(Severity.ERROR, dslAnnotation,
63: message, vp.fixerFactory(vp.getValAnnotation()
64: .fixers()));
65: }
66:
67: }
68:
69: }
|