KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > provisional > validation > RegexStringValidator


1 /*
2  * Copyright (C) 2005 db4objects Inc. http://www.db4o.com
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * db4objects - Initial API and implementation
11  */

12
13 package org.eclipse.jface.internal.databinding.provisional.validation;
14
15 import java.util.regex.Matcher JavaDoc;
16 import java.util.regex.Pattern JavaDoc;
17
18
19 /**
20  * RegularExpressionVerifier. A generic String validator that uses regular expressions to
21  * specify validation rules.
22  */

23 public class RegexStringValidator implements IValidator {
24     
25     private Pattern JavaDoc fragmentRegex;
26     private Pattern JavaDoc fullValueRegex;
27     private String JavaDoc hint;
28     
29     /**
30      * Constructor RegularExpressionValidator. Construct a string validator based on regular
31      * expressions.
32      *
33      * Verify input using regular expressions.
34      *
35      * @param partiallyValidRegex A regex that matches iff the value is partially valid
36      * @param fullyValidRegex A regex that matches iff the value is fully valid
37      * @param hint The hint to display if the value is invalid
38      */

39     public RegexStringValidator(String JavaDoc partiallyValidRegex,
40             String JavaDoc fullyValidRegex, String JavaDoc hint) {
41         super();
42         this.fragmentRegex = Pattern.compile(partiallyValidRegex);
43         this.fullValueRegex = Pattern.compile(fullyValidRegex);
44         this.hint = hint;
45     }
46     
47     /* (non-Javadoc)
48      * @see org.eclipse.jface.databinding.validator.IValidator#isPartiallyValid(java.lang.Object)
49      */

50     public ValidationError isPartiallyValid(Object JavaDoc fragment) {
51         Matcher JavaDoc matcher = fragmentRegex.matcher((String JavaDoc)fragment);
52         if (matcher.find())
53             return null;
54
55         return ValidationError.error(hint);
56     }
57
58     /* (non-Javadoc)
59      * @see org.eclipse.jface.viewers.ICellEditorValidator#isValid(java.lang.Object)
60      */

61     public ValidationError isValid(Object JavaDoc value) {
62         String JavaDoc stringValue = (String JavaDoc) value;
63         Matcher JavaDoc matcher = fullValueRegex.matcher(stringValue);
64         if (matcher.find())
65             return null;
66
67         return ValidationError.error(hint);
68     }
69
70 }
71
Popular Tags