KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > sam > data > RegexValidator


1 /*
2  * Copyright (C) 2003 Stefan Armbruster [stefan@armbruster-it.de]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: RegexValidator.java,v 1.3 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.sam.data;
21
22 import java.util.*;
23 //import java.util.regex.*;
24
import gnu.regexp.*;
25
26 import org.enhydra.barracuda.core.forms.*;
27 import org.enhydra.barracuda.plankton.*;
28
29 /** This validator ensures that a value matches a given regexp.
30  * For compatibility with JDK 1.3, all references to java.util.regex are replaced by gnu.regexp
31  */

32 public class RegexValidator extends DefaultFormValidator {
33
34      // JDK 1.4: uncomment the next line and comment the following line out
35
//protected Pattern re;
36
protected RE re;
37
38     /**
39      * Public constructor.
40      *
41      * @param re Pattern to be matched
42      */

43      // JDK 1.4: uncomment the next line and comment the following line out
44
//public RegexValidator(Pattern re ) {
45
public RegexValidator(RE re ) {
46         super();
47         this.re = re;
48     }
49
50     /**
51      * Public constructor.
52      *
53      * @param re Pattern to be matched
54      * @param ierrmsg the message associated with this error
55      */

56      // JDK 1.4: uncomment the next line and comment the following line out
57
//public RegexValidator(Pattern re, String ierrmsg) {
58
public RegexValidator(RE re, String JavaDoc ierrmsg) {
59         super(ierrmsg);
60         this.re = re;
61     }
62
63     /**
64      * Validate a FormElement to make sure a given pattern is matched
65      * Validation is only supported for FormType.STRING
66      *
67      * @param element the form element to be validated
68      * @param deferExceptions do we want to deferValidation exceptions
69      * and attempt to validate all elements so that we can process
70      * all the exceptions at once
71      * @throws ValidationException if the element is not valid
72      */

73     public void validateFormElement(Object JavaDoc val, FormElement element, boolean deferExceptions) throws ValidationException {
74         if (this.isNull(val, element))
75             return;
76
77         if (element==null) throw new ValidationException(val, "Object val:"+val+" is associated with a null FormElement");
78         if (!element.getType().equals(FormType.STRING)) throw new ValidationException(val, "Unsupported validation: "+val+" is not of FormType.String and cannot be validated by this validator");
79
80         String JavaDoc s = val.toString();
81      // JDK 1.4: uncomment the next line and comment the following line out
82
//if (!re.matcher(s).matches()) {
83
if (!re.isMatch(s)) {
84      // JDK 1.4: uncomment the next line and comment the following line out
85
//throw this.generateException(element, deferExceptions, "{" + val + "}" + " does not match Pattern " + re.pattern());
86
throw this.generateException(element, deferExceptions, "{" + val + "}" + " does not match Pattern " + re.toString());
87         }
88     }
89
90 }
91
Popular Tags