Source code: com/gammastream/validity/GSVStringMethods.java
1 package com.gammastream.validity;
2
3 import com.webobjects.foundation.*;
4 import com.webobjects.appserver.*;
5 import com.webobjects.eocontrol.*;
6 import com.webobjects.eoaccess.*;
7
8 import java.math.BigDecimal;
9 import java.net.InetAddress;
10 import java.net.UnknownHostException;
11 import java.io.StringReader;
12
13 import com.gammastream.gammacore.gammatext.*;
14
15 /**
16 * This class provides a set of predefined rules for performing
17 * validation on <code>Strings</code>. These rules are part of
18 * the default set of 'QuickRules'.
19 *
20 * @author GammaStream Technologies, Inc.
21 */
22 public class GSVStringMethods {
23
24 /**
25 * For programatic purposes, we include this constant which is used for the <code>compareTo</code> method.
26 */
27 public final static String EQUAL = "==";
28
29 /**
30 * For programatic purposes, we include this constant which is used for the <code>compareTo</code> method.
31 */
32 public final static String NOT_EQUAL = "!=";
33
34 /**
35 * For programatic purposes, we include this constant which is used for the <code>compareTo</code> method.
36 */
37 public final static String GREATER_THAN = ">";
38
39 /**
40 * For programatic purposes, we include this constant which is used for the <code>compareTo</code> method.
41 */
42 public final static String GREATER_EQUAL = ">=";
43
44 /**
45 * For programatic purposes, we include this constant which is used for the <code>compareTo</code> method.
46 */
47 public final static String LESS_THAN = "<";
48
49 /**
50 * For programatic purposes, we include this constant which is used for the <code>compareTo</code> method.
51 */
52 public final static String LESS_EQUAL = "<=";
53
54 /**
55 * Determines whether the specified attribute is empty.
56 * <br>An empty String is defined as a String that contains at least one non-white-space character.
57 * <br>(i.e. This method will return <code>true</code> if the attribute only contains spaces, CRs, NLs, etc.)
58 *
59 * @param object The object whose attribute is being validated.
60 * @param attribute The attribute being validated.
61 * @param key The key used to access the attribute.
62 * @param params The param dictionary which must contain the above mentioned key-value pairs.
63 *
64 * @return <code>true</code> if the string is empty; otherwise, <code>false</code>
65 */
66 public final static boolean isStringEmpty(Object object, Object attribute, String key, NSDictionary params){
67 if(attribute instanceof String){
68 return GSTextUtilities.isStringEmpty( (String)attribute );
69 }
70 return true;
71 }
72
73 /**
74 * One of the many 'mutators' which never fail, unless of course, an exception is thrown.
75 * <br>A mutator simply modifies (or mutates) the attribute is some way.
76 * <br>In this case, it converts the <code>String</code> to all upper case characters.
77 *
78 * @param object The object whose attribute is being validated.
79 * @param attribute The attribute being validated.
80 * @param key The key used to access the attribute.
81 * @param params The param dictionary which must contain the above mentioned key-value pairs.
82 *
83 * @return always <code>true</code>
84 */
85 public final static boolean toUpperCase(Object object, Object attribute, String key, NSDictionary params){
86 if( (attribute instanceof String) && (attribute != null) ){
87 NSKeyValueCoding.Utility.takeValueForKey(object, ((String)attribute).toUpperCase(), key);
88 }
89 return true;
90 }
91
92 /**
93 * One of the many 'mutators' which never fail, unless of course, an exception is thrown.
94 * <br>A mutator simply modifies (or mutates) the attribute is some way.
95 * <br>In this case, it converts the <code>String</code> to all lower case characters.
96 *
97 * @param object The object whose attribute is being validated.
98 * @param attribute The attribute being validated.
99 * @param key The key used to access the attribute.
100 * @param params The param dictionary which must contain the above mentioned key-value pairs.
101 *
102 * @return always <code>true</code>
103 */
104 public final static boolean toLowerCase(Object object, Object attribute, String key, NSDictionary params){
105 if( (attribute instanceof String) && (attribute != null) ){
106 NSKeyValueCoding.Utility.takeValueForKey(object, ((String)attribute).toLowerCase(), key);
107 }
108 return true;
109 }
110
111 /**
112 * Compares the length of the attribute.
113 * <br>NSDictionary key/value pairs:
114 * <br>Operator=""
115 * <br>RightOperand=""
116 *
117 * @return true or false
118 * @param object object being validated.
119 * @param attribute attribute being validated.
120 * @param key key for attribute.
121 * @param params extra parameters.
122 */
123
124 /**
125 * Determines whether the specified string is of a specified length. (i.e. 'x' characters long)
126 * <br>
127 * <br>The required key-value pairs include:
128 * <br>"Operator" = One of the defined operator constants.
129 * <br>"RightOperand" = A number representing the number of characters one is interested.
130 * <br>
131 *
132 * @param object The object whose attribute is being validated.
133 * @param attribute The attribute being validated.
134 * @param key The key used to access the attribute.
135 * @param params The param dictionary which must contain the above mentioned key-value pairs.
136 *
137 * @return <code>true</code> if the comparison succeeds; otherwise, <code>false</code>
138 */
139 public final static boolean length(Object object, Object attribute, String key, NSDictionary params){
140 if(attribute instanceof String){
141 String sign = (String)params.objectForKey("Operator");
142 String number = (String)params.objectForKey("RightOperand");
143 BigDecimal left = new BigDecimal( ((String)attribute).length() );
144 BigDecimal right = new BigDecimal(number);
145 int comparisonValue = left.compareTo( right );
146
147 if( sign.equals(GSVStringMethods.EQUAL) ){
148 return ( comparisonValue == 0 );
149 } else if( sign.equals(GSVStringMethods.NOT_EQUAL) ){
150 return ( comparisonValue != 0 );
151 } else if( sign.equals(GSVStringMethods.GREATER_THAN) ){
152 return ( comparisonValue == 1 );
153 } else if( sign.equals(GSVStringMethods.GREATER_EQUAL) ){
154 return ( (comparisonValue == 1) || (comparisonValue == 0) );
155 } else if( sign.equals(GSVStringMethods.LESS_THAN) ){
156 return ( comparisonValue == -1 );
157 } else if( sign.equals(GSVStringMethods.LESS_EQUAL) ){
158 return ( (comparisonValue == -1) || (comparisonValue == 0) );
159 }
160 }
161 return false;
162 }
163
164
165 /**
166 * Verifies that the attribute contains the specified string.
167 * <br>
168 * <br>The required key-value pairs include:
169 * <br>"Contains" = The string of interest.
170 * <br>
171 *
172 * @param object The object whose attribute is being validated.
173 * @param attribute The attribute being validated.
174 * @param key The key used to access the attribute.
175 * @param params The param dictionary which must contain the above mentioned key-value pairs.
176 *
177 * @return <code>true</code> if the attribute contains the specified string; otherwise, <code>false</code>
178 */
179 public final static boolean contains(Object object, Object attribute, String key, NSDictionary params){
180 if(attribute instanceof String && params != null){
181 String what = (String)params.objectForKey("Contains");
182 String attributeAtString = (String)attribute;
183 return ( attributeAtString.indexOf(what) != -1 );
184 }
185 return false;
186 }
187
188 /**
189 * Verifies that the attribute ends with the specified string.
190 * <br>
191 * <br>The required key-value pairs include:
192 * <br>"EndsWith" = The string of interest.
193 * <br>
194 *
195 * @param object The object whose attribute is being validated.
196 * @param attribute The attribute being validated.
197 * @param key The key used to access the attribute.
198 * @param params The param dictionary which must contain the above mentioned key-value pairs.
199 *
200 * @return <code>true</code> if the attribute ends with the specified string; otherwise, <code>false</code>
201 */
202 public final static boolean endsWith(Object object, Object attribute, String key, NSDictionary params){
203 if(attribute instanceof String){
204 return(((String)attribute).endsWith((String)params.objectForKey("EndsWith")));
205 }
206 return false;
207 }
208
209 /**
210 * Verifies that the attribute starts with the specified string.
211 * <br>
212 * <br>The required key-value pairs include:
213 * <br>"StartsWith" = The string of interest.
214 * <br>
215 *
216 * @param object The object whose attribute is being validated.
217 * @param attribute The attribute being validated.
218 * @param key The key used to access the attribute.
219 * @param params The param dictionary which must contain the above mentioned key-value pairs.
220 *
221 * @return <code>true</code> if the attribute starts with the specified string; otherwise, <code>false</code>
222 */
223 public final static boolean startsWith(Object object, Object attribute, String key, NSDictionary params){
224 if(attribute instanceof String)
225 return(((String)attribute).startsWith((String)params.objectForKey("StartsWith")));
226 return false;
227 }
228
229 /**
230 * Verifies that the attribute contains only alphabetic characters. (i.e. 'a'-'z' || 'A'-'Z')
231 *
232 * @param object The object whose attribute is being validated.
233 * @param attribute The attribute being validated.
234 * @param key The key used to access the attribute.
235 * @param params The param dictionary which must contain the above mentioned key-value pairs.
236 *
237 * @return <code>true</code> if the string only contains alphabetic characters; otherwise, <code>false</code>
238 */
239 public final static boolean isAlphabetic(Object object, Object attribute, String key, NSDictionary params){
240 if(attribute instanceof String){
241 return(GSTextUtilities.isAlphabetic((String)attribute));
242 }
243 return false;
244 }
245
246 /**
247 * Verifies that the attribute contains only letters or numbers. (i.e. 'a'-'z' || 'A'-'Z' || '0'-'9')
248 *
249 * @param object The object whose attribute is being validated.
250 * @param attribute The attribute being validated.
251 * @param key The key used to access the attribute.
252 * @param params The param dictionary which must contain the above mentioned key-value pairs.
253 *
254 * @return <code>true</code> if the string only contains letters or numbers; otherwise, <code>false</code>
255 */
256 public final static boolean isAlphaNumeric(Object object, Object attribute, String key, NSDictionary params){
257 if(attribute instanceof String){
258 return(GSTextUtilities.isAlphaNumeric((String)attribute));
259 }
260 return false;
261 }
262
263 /**
264 * One of the many 'mutators' which never fail, unless of course, an exception is thrown.
265 * <br>A mutator simply modifies (or mutates) the attribute is some way.
266 * <br>In this case, it strips any HTML out of the <code>String</code>.
267 * <br>HTML is considered anything (and including) '<' and '>'.
268 *
269 * @param object The object whose attribute is being validated.
270 * @param attribute The attribute being validated.
271 * @param key The key used to access the attribute.
272 * @param params The param dictionary which must contain the above mentioned key-value pairs.
273 *
274 * @return always <code>true</code>
275 */
276 public final static boolean stripHTML(Object object, Object attribute, String key, NSDictionary params){
277 if(attribute instanceof String && attribute !=null){
278 NSKeyValueCoding.Utility.takeValueForKey(object,GSTextUtilities.stringStrippedOfHTML((String)attribute), key);
279 }
280 return true;
281 }
282
283 /**
284 * Performs a string comparision using the specified params dictionary.
285 * <br>
286 * <br>The required key-value pairs include:
287 * <br>"Operator" = The specified operator string. (i.e. "==", "!=", ">", ">=", "<", or "<=" )
288 * <br>"RightOperand" = The <code>String</code> to compare the attribute to.
289 * <br>
290 *
291 * @param object The object whose attribute is being validated.
292 * @param attribute The attribute being validated.
293 * @param key The key used to access the attribute.
294 * @param params The param dictionary which must contain the above mentioned key-value pairs.
295 *
296 * @return <code>true</code> if the comparision succeeds; otherwise, <code>false</code>
297 */
298 public final static boolean compareTo(Object object, Object attribute, String key, NSDictionary params){
299 if(attribute instanceof String){
300 String sign = (String)params.objectForKey("Operator");
301 String right = (String)params.objectForKey("RightOperand");
302 int comparisonValue = ((String)attribute).compareTo(right);
303
304 if(sign.equals(GSVStringMethods.EQUAL)){
305 return(comparisonValue==0);
306 }else if(sign.equals(GSVStringMethods.NOT_EQUAL)){
307 return(comparisonValue!=0);
308 }else if(sign.equals(GSVStringMethods.GREATER_THAN)){
309 return(comparisonValue>0);
310 }else if(sign.equals(GSVStringMethods.GREATER_EQUAL)){
311 return(comparisonValue>=0);
312 }else if(sign.equals(GSVStringMethods.LESS_THAN)){
313 return(comparisonValue<0);
314 }else if(sign.equals(GSVStringMethods.LESS_EQUAL)){
315 return(comparisonValue<=0);
316 }
317 }
318 return false;
319 }
320
321 /**
322 * Verifies the attribute is a valid url. (Proper Syntax)
323 * <br>The validator will verify, 'http', 'ftp', 'mailto', 'telnet', and 'file' urls.
324 * <br>
325 *
326 * @param object The object whose attribute is being validated.
327 * @param attribute The attribute being validated.
328 * @param key The key used to access the attribute.
329 * @param params The param dictionary which must contain the above mentioned key-value pairs.
330 *
331 * @return <code>true</code> if the url is valid; otherwise, <code>false</code>
332 */
333 public final static boolean isValidURL(Object object, Object attribute, String key, NSDictionary params){
334 if(attribute instanceof String) {
335 try {
336 java.net.URL url = new java.net.URL((String)attribute);
337 // accept only http-urls
338 return url.getProtocol().equals("http");
339
340 } catch(java.net.MalformedURLException mue) {
341 //ignore
342 NSLog.err.appendln(mue.getMessage());
343 }
344 }
345 return false;
346 }
347
348 /**
349 * Verifies the attribute is a valid email address. (Proper Syntax)
350 *
351 * @param object The object whose attribute is being validated.
352 * @param attribute The attribute being validated.
353 * @param key The key used to access the attribute.
354 * @param params The param dictionary which must contain the above mentioned key-value pairs.
355 *
356 * @return <code>true</code> if the address is valid; otherwise, <code>false</code>
357 */
358 public final static boolean isValidEmailAddress(Object object, Object attribute, String key, NSDictionary params){
359 if(attribute instanceof String) {
360 try {
361 javax.mail.internet.InternetAddress add = new javax.mail.internet.InternetAddress((String)attribute);
362 add.validate();
363 // get sure it is only the address and no personal
364 return (add.getPersonal() == null);
365
366 } catch(javax.mail.internet.AddressException ae) {
367 //ignore
368 NSLog.err.appendln(ae.getMessage());
369 }
370 }
371 return false;
372 }
373
374
375 }