View Javadoc

1   /*
2   Copyright (C) 2000 - 2007 Grid Systems, S.A.
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2, as
6   published by the Free Software Foundation.
7   
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17  
18  /*
19   * Project: KernelConfigurator
20   * Created on 04-mar-2004
21   *
22   * Copyright (c)2003 Grid Systems
23   */
24  package com.gridsystems.config.tools;
25  
26  import java.util.regex.Matcher;
27  import java.util.regex.Pattern;
28  
29  /**
30   * Performs a validation based on the pattern specified on construction.
31   * <p>
32   * The pattern follows the syntax specified in the {@link java.util.regex.Pattern}
33   * documentation.
34   *
35   * @author <a href="mailto:rruiz@gridsystems.com">Rodrigo Ruiz Aguayo</a>
36   * @version 1.0
37   */
38  public class PatternVerifier extends TextVerifier {
39    /**
40     * Identifier for errors detected by this verifier.
41     */
42    public static final String ID_ERROR = "PatternMatchError";
43  
44    /**
45     * The pattern to match to.
46     */
47    private Pattern p;
48  
49    /**
50     * Identifier of the error detected by this verifier.
51     */
52    protected String id;
53  
54    /**
55     * Creates a new instance with the specified pattern string and default flags.
56     *
57     * @param support The ErrorSupport instance
58     * @param id      The error id
59     * @param pattern The pattern string to use
60     */
61    public PatternVerifier(ErrorSupport support, String id, String pattern) {
62      this(support, id, Pattern.compile(pattern));
63    }
64  
65    /**
66     * Creates an instance.
67     *
68     * @param support The error support instance
69     * @param id      The error id
70     * @param pattern The pattern string to use
71     * @param flags   The flags for pattern compilation
72     */
73    public PatternVerifier(ErrorSupport support, String id, String pattern, int flags) {
74      this(support, id, Pattern.compile(pattern, flags));
75    }
76  
77    /**
78     * Creates a new instance with the specified pattern.
79     *
80     * @param support The error support instance
81     * @param id      The error id
82     * @param pattern The pattern to use
83     */
84    public PatternVerifier(ErrorSupport support, String id, Pattern pattern) {
85      super(support);
86      this.id = id;
87      this.p = pattern;
88    }
89  
90    /**
91     * {@inheritDoc}
92     */
93    public boolean verify(String text) {
94      Matcher m = p.matcher(text);
95      boolean valid = m.matches();
96  
97      this.updateError(id, !valid);
98      return valid;
99    }
100 
101   /**
102    * @see TextVerifier#cleanErrors()
103    */
104   public void cleanErrors() {
105     updateError(id, false);
106   }
107 }