View Javadoc

1   /*
2    *******************************************************************************
3    * Copyright (c) 2005 Chris Rose and AIMedia
4    * All rights reserved. AbstractRegexKeyValidator and the accompanying materials
5    * are made available under the terms of the Common Public License v1.0
6    * which accompanies this distribution, and is available at
7    * http://www.eclipse.org/legal/cpl-v10.html
8    * 
9    * Contributors:
10   *     Chris Rose
11   *******************************************************************************/
12  package ca.spaz.cron.config;
13  
14  import java.util.regex.*;
15  
16  public abstract class AbstractRegexKeyValidator implements PropertyValidator {
17  
18     protected Matcher getMatcher(String string) {
19        if (null == string) {
20           throw new IllegalArgumentException("Null strings are not allowed");
21        }
22        return Pattern.compile(getExpression()).matcher(string);
23     }
24     
25     /* (non-Javadoc)
26      * @see ca.spaz.cron.config.PropertyValidator#isValid(java.lang.String, java.lang.String)
27      */
28     public boolean isValid(String key, String value) {
29        boolean valid = false;
30        if (getMatcher(key).find()) {
31           valid = doIsValid(key, value);
32        }
33        return valid;
34     }
35     
36     /***
37      * This method will be called only in the case where the pattern matches.  This
38      * can be assumed to be a precondition of the method call.
39      * 
40      * @param key The matching key.
41      * @param value The value being checked.
42      * @return <code>true</code> if <code>value</code> is a valid value for
43      * <code>key</code>, <code>false</code> otherwise.
44      */
45     protected abstract boolean doIsValid(String key, String value);
46     
47     /***
48      * Get the regular expression to match the key on.
49      * @return a string representation of a regular expression.
50      */
51     protected abstract String getExpression();
52     
53  }