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  package com.gridsystems.config.tools;
19  
20  import java.text.MessageFormat;
21  
22  import com.gridsystems.config.app.UI;
23  
24  /**
25   * Verifies that a password field has a proper length and no forbiden symbols.
26   *
27   * @author SJM
28   */
29  public class PasswordVerifier extends TextVerifier {
30    /**
31     * Too short error key.
32     */
33    public static final String TOO_SHORT_ERROR = "errors.passwordTooShort";
34    /**
35     * Too long error key.
36     */
37    public static final String TOO_LONG_ERROR = "errors.passwordTooLong";
38    /**
39     * Bad characters in password key.
40     */
41    public static final String BAD_CHAR_ERROR = "errors.badCharInPassword";
42  
43    /**
44     * All possible errors.
45     */
46    private static String[] codeErrors = new String[] {
47      TOO_SHORT_ERROR,  TOO_LONG_ERROR, BAD_CHAR_ERROR
48    };
49  
50    /**
51     * The minimum password length.
52     */
53    private int minLength;
54  
55    /**
56     * The maximum password length.
57     */
58    private int maxLength;
59  
60    /**
61     * Creates an instance.
62     *
63     * @param support   The ErrorSupport instance
64     * @param minLength Password minimum length
65     * @param maxLength Password maximum length
66     */
67    public PasswordVerifier(ErrorSupport support, int minLength, int maxLength) {
68      super(support);
69      this.minLength = minLength;
70      this.maxLength = maxLength;
71      initSupport();
72    }
73  
74    /**
75     * Initializes the default error message support.
76     */
77    private void initSupport() {
78      //Error messages
79      String minLengthMessage = MessageFormat.format(UI.getString(codeErrors[0]),
80                                new Object[] {"" + minLength});
81      String maxLengthMessage = MessageFormat.format(UI.getString(codeErrors[1]),
82                                new Object[] {"" + maxLength});
83  
84      support.addError(codeErrors[0], minLengthMessage);
85      support.addError(codeErrors[1], maxLengthMessage);
86      support.addError(codeErrors[2], UI.getString(codeErrors[2]));
87    }
88  
89    /**
90     * {@inheritDoc}
91     */
92    public boolean verify(String text) {
93      int length = text.length();
94  
95      // Verify minimum length
96      boolean v1 = (minLength == 0 || length >= minLength);
97      updateError(TOO_SHORT_ERROR, !v1);
98  
99      // Verify maximum length
100     boolean v2 = (maxLength == 0 || length <= maxLength);
101     updateError(TOO_LONG_ERROR, !v2);
102 
103     //Check valid characters
104     boolean v3 = true;
105     for (int i = 0; v3 && i < length; i++) {
106       char ch = text.charAt(i);
107       // All ASCII printable characters but ' ', '<', and '>' are allowed
108       v3 = (ch > 32 && ch < 128 && ch != '<' && ch != '>');
109     }
110     updateError(BAD_CHAR_ERROR, !v3);
111 
112     return v1 && v2 && v3;
113   }
114 
115   /**
116    * @see TextVerifier#cleanErrors()
117    */
118   public void cleanErrors() {
119     updateError(TOO_SHORT_ERROR, false);
120     updateError(TOO_LONG_ERROR, false);
121     updateError(BAD_CHAR_ERROR, false);
122   }
123 }