View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.portals.gems.util;
18  
19  import java.text.ParseException;
20  import java.text.SimpleDateFormat;
21  import java.util.Date;
22  
23  import org.apache.commons.lang.StringUtils;
24  
25  /***
26   * ValidationHelper using regular expressions
27   *
28   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
29   * @version $Id: $
30   */
31  public abstract class ValidationHelper
32  {
33      public static final SimpleDateFormat EUROPEAN_DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy");
34      public static final SimpleDateFormat EUROPEAN_DATETIME_FORMAT = new SimpleDateFormat("dd-MM-yyyy HH:mm");
35      public static final SimpleDateFormat AMERICAN_DATE_FORMAT = new SimpleDateFormat("MM-dd-yyyy");
36      public static final SimpleDateFormat AMERICAN_DATETIME_FORMAT = new SimpleDateFormat("MM-dd-yyyy HH:mm");
37      
38      /***
39       * Tests that the input string contains only alpha numeric or white spaces
40       * <P>
41       * @param evalString The string that is to be evaluated
42       * @param required indicates whether the field is required or not
43       * @return True if the input is alpha numeric, false otherwise.
44       **/
45      public static boolean isAlphaNumeric(String evalString, boolean required)
46      {
47          if (StringUtils.isEmpty(evalString))
48          {
49              if (true == required)
50              {
51                  return false;
52              }
53              return true;
54          }        
55          return evalString.matches("^[//w//s]+$");
56      }
57  
58      public static boolean isAlphaNumeric(String evalString, boolean required, int maxLength)
59      {
60          if (isTooLong(evalString, maxLength))
61          {
62              return false;
63          }
64          return isAlphaNumeric(evalString, required);
65      }
66  
67      public static boolean isLooseAlphaNumeric(String evalString, boolean required)
68      {
69          if (StringUtils.isEmpty(evalString))
70          {
71              if (true == required)
72              {
73                  return false;
74              }
75              return true;
76          }
77          return evalString.matches("^[//w//s//.//,/////-//(//)//+]+$");
78      }
79  
80      public static boolean isLooseAlphaNumeric(String evalString, boolean required, int maxLength)
81      {
82          if (isTooLong(evalString, maxLength))
83          {
84              return false;
85          }
86          return isLooseAlphaNumeric(evalString, required);
87      }
88          
89      /***
90       * Tests that the input string contains only numeric
91       * <P>
92       * @param evalString The string that is to be evaluated
93       * @return True if the input is numeric, false otherwise.
94       **/
95      public static boolean isDecimal(String evalString, boolean required)
96      {
97          if (StringUtils.isEmpty(evalString))
98          {
99              if (true == required)
100             {
101                 return false;
102             }
103             return true;
104         }
105         return evalString.matches("^(//d+//.)?//d+$");
106     }
107     
108     public static boolean isDecimal(String evalString, boolean required, int maxLength)
109     {
110         if (isTooLong(evalString, maxLength))
111         {
112             return false;
113         }
114         return isDecimal(evalString, required);
115     }
116 
117     /***
118      * Tests that the input string contains only an integer
119      * <P>
120      * @param evalString The string that is to be evaluated
121      * @return True if the input is numeric, false otherwise.
122      **/
123     public static boolean isInteger (String evalString, boolean required)
124     {
125         if (StringUtils.isEmpty(evalString))
126         {
127             if (true == required)
128             {
129                 return false;
130             }
131             return true;
132         }
133         return evalString.matches("^//d+$");
134     }
135     
136     public static boolean isInteger(String evalString, boolean required, int maxLength)
137     {
138         if (isTooLong(evalString, maxLength))
139         {
140             return false;
141         }
142         return isInteger(evalString, required);
143     }
144 
145     /***
146      * Tests that the input string contains a valid email addess
147      * <P>
148      * @param evalString The string that is to be evaluated
149      * @return True if the input is a valid email address.
150      **/
151     public static boolean isEmailAddress(String evalString, boolean required)
152     {
153         if (StringUtils.isEmpty(evalString))
154         {
155             if (true == required)
156             {
157                 return false;
158             }
159             return true;
160         }
161         return evalString.matches("^(?://w[//w-]*//.)*//w[//w-]*@(?://w[//w-]*//.)+//w[//w-]*$");
162     }
163     
164     public static boolean isEmailAddress(String evalString, boolean required, int maxLength)
165     {
166         if (isTooLong(evalString, maxLength))
167         {
168             return false;
169         }
170         return isEmailAddress(evalString, required);
171     }
172     
173     /***
174      * Tests that the input string contains a valid URL
175      * <P>
176      * @param evalString The string that is to be evaluated
177      * @return True if the input is a valid URL.
178      **/
179     public static boolean isURL(String evalString, boolean required)
180     {
181         try
182         {
183             if (StringUtils.isEmpty(evalString))
184             {
185                 if (true == required)
186                 {
187                     return false;
188                 }
189                 return true;
190             }
191             
192             //URL url = new URL(evalString);
193 
194             /*
195             Perl5Util util = new Perl5Util();
196             System.out.println("looking at " +evalString);
197             return evalString.matches("^[//w%?-_~]$", evalString);
198              */
199             //Object content = url.getContent();
200             //System.out.println("url contains :["+content+"]");
201             return true;
202         }
203         catch (Exception e)
204         {
205             System.err.println(evalString+" is not a valid URL: "+ e);
206             return false;
207         }
208     }
209 
210     public static boolean isURL(String evalString, boolean required, int maxLength)
211     {
212         if (isTooLong(evalString, maxLength))
213         {
214             return false;
215         }
216         return isURL(evalString, required);
217     }
218 
219     public static boolean isValidIdentifier(String folderName)
220     {
221         boolean valid = true;
222 
223         char[] chars = folderName.toCharArray();
224         for (int ix = 0; ix < chars.length; ix++)
225         {
226             if (!Character.isJavaIdentifierPart(chars[ix]))
227             {
228                 valid = false; 
229                 break;
230             }
231         }
232         return valid;
233     }
234 
235     public static boolean isTooLong(String evalString, int maxLength)
236     {
237         if (null != evalString)
238         {
239             return (evalString.length() > maxLength);
240         }
241         return false;
242     }
243 
244     public static boolean isPhoneNumber(String evalString, boolean required, int maxLength)
245     {
246         if (isTooLong(evalString, maxLength))
247         {
248             return false;
249         }
250         return isPhoneNumber(evalString, required);
251     }
252     
253     public static boolean isPhoneNumber(String evalString, boolean required)
254     {
255         if (StringUtils.isEmpty(evalString))
256         {
257             if (true == required)
258             {
259                 return false;
260             }
261             return true;
262         }
263         //return evalString.matches("[(][0-9]{3}[)][ ]*[0-9]{3}-[0-9]{4}", evalString);
264         return evalString.matches("(//+[0-9]{2})?(//({0,1}[0-9]{3}//){0,1} {0,1}[ |-]{0,1} {0,1}){0,1}[0-9]{3,5}[ |-][0-9]{4,6}");
265     }
266 
267     public static Date parseDate(String formatted)
268     {
269         Date date = null;
270         if (null == formatted)
271         {
272             return null;
273         }
274         try
275         {
276             synchronized (EUROPEAN_DATE_FORMAT)
277             {
278                 date = EUROPEAN_DATE_FORMAT.parse(formatted);
279             }
280         }
281         catch (ParseException e)
282         {
283             try
284             {
285                 synchronized (AMERICAN_DATE_FORMAT)
286                 {
287                     date = AMERICAN_DATE_FORMAT.parse(formatted);
288                 }
289             }
290             catch (ParseException ee)
291             {
292             }            
293         }
294         return date;
295     }
296 
297     public static Date parseDatetime(String formatted)
298     {
299         Date date = null;
300         if (null == formatted)
301         {
302             return null;
303         }
304         
305         try
306         {
307             synchronized (EUROPEAN_DATETIME_FORMAT)
308             {
309                 date = EUROPEAN_DATETIME_FORMAT.parse(formatted);
310             }
311         }
312         catch (ParseException e)
313         {
314             try
315             {
316                 synchronized (AMERICAN_DATETIME_FORMAT)
317                 {
318                     date = AMERICAN_DATETIME_FORMAT.parse(formatted);
319                 }
320             }
321             catch (ParseException ee)
322             {
323             }            
324         }
325         return date;
326     }
327     
328     public static String formatEuropeanDate(Date date)
329     {
330         if (null == date)
331         {
332             return null;
333         }
334         synchronized (EUROPEAN_DATE_FORMAT)
335         {
336             return EUROPEAN_DATE_FORMAT.format(date);        
337         }
338     }
339     
340     public static String formatAmericanDate(Date date)
341     {
342         if (null == date)
343         {
344             return null;
345         }        
346         synchronized (AMERICAN_DATE_FORMAT)
347         {
348             return AMERICAN_DATE_FORMAT.format(date);        
349         }
350     }
351 
352     public static String formatEuropeanDatetime(Date date)
353     {
354         if (null == date)
355         {
356             return null;
357         }        
358         synchronized (EUROPEAN_DATETIME_FORMAT)
359         {
360             return EUROPEAN_DATETIME_FORMAT.format(date);        
361         }
362     }
363     
364     public static String formatAmericanDatetime(Date date)
365     {
366         if (null == date)
367         {
368             return null;
369         }        
370         synchronized (AMERICAN_DATETIME_FORMAT)
371         {
372             return AMERICAN_DATETIME_FORMAT.format(date);        
373         }
374     }
375     
376     public static boolean isValidDate(String formatted)
377     {
378         if (formatted == null || formatted.trim().length() == 0)
379             return true;
380             
381         try
382         {
383             EUROPEAN_DATE_FORMAT.parse(formatted);
384         }
385         catch (ParseException e)
386         {
387             try
388             {
389                 synchronized (AMERICAN_DATE_FORMAT)
390                 {
391                     AMERICAN_DATE_FORMAT.parse(formatted);
392                 }
393             }
394             catch (ParseException ee)
395             {
396                 return false;
397             }            
398         }
399         return true;        
400     }
401     
402     public static boolean isValidDatetime(String formatted)
403     {
404         if (formatted == null || formatted.trim().length() == 0)
405             return true;
406             
407         try
408         {
409             synchronized (EUROPEAN_DATETIME_FORMAT)
410             {
411                 EUROPEAN_DATETIME_FORMAT.parse(formatted);
412             }
413         }
414         catch (ParseException e)
415         {
416             try
417             {
418                 synchronized (AMERICAN_DATETIME_FORMAT)
419                 {
420                     AMERICAN_DATETIME_FORMAT.parse(formatted);
421                 }
422             }
423             catch (ParseException ee)
424             {
425                 return false;
426             }            
427         }
428         return true;        
429     }
430 
431     public static boolean isAny(String evalString, boolean required)
432     {
433         if (StringUtils.isEmpty(evalString))
434         {
435             if (true == required)
436             {
437                 return false;
438             }
439             return true;
440         }
441         return true;
442     }
443 
444     public static boolean isAny(String evalString, boolean required, int maxLength)
445     {
446         if (isTooLong(evalString, maxLength))
447         {
448             return false;
449         }
450         return isAny(evalString, required);
451     }
452     
453 }