1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
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  
26  
27  
28  
29  public class PasswordVerifier extends TextVerifier {
30    
31  
32  
33    public static final String TOO_SHORT_ERROR = "errors.passwordTooShort";
34    
35  
36  
37    public static final String TOO_LONG_ERROR = "errors.passwordTooLong";
38    
39  
40  
41    public static final String BAD_CHAR_ERROR = "errors.badCharInPassword";
42  
43    
44  
45  
46    private static String[] codeErrors = new String[] {
47      TOO_SHORT_ERROR,  TOO_LONG_ERROR, BAD_CHAR_ERROR
48    };
49  
50    
51  
52  
53    private int minLength;
54  
55    
56  
57  
58    private int maxLength;
59  
60    
61  
62  
63  
64  
65  
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  
76  
77    private void initSupport() {
78      
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  
91  
92    public boolean verify(String text) {
93      int length = text.length();
94  
95      
96      boolean v1 = (minLength == 0 || length >= minLength);
97      updateError(TOO_SHORT_ERROR, !v1);
98  
99      
100     boolean v2 = (maxLength == 0 || length <= maxLength);
101     updateError(TOO_LONG_ERROR, !v2);
102 
103     
104     boolean v3 = true;
105     for (int i = 0; v3 && i < length; i++) {
106       char ch = text.charAt(i);
107       
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 
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 }