1 /*
2 * Copyright 2000-2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.apache.jetspeed.util;
17
18 import org.apache.oro.text.perl.*;
19 //import java.util.regex.*;
20
21 import java.net.*;
22 import org.apache.commons.lang.StringUtils;
23
24 /***
25 * <P>Data Validation functions using Regex</P>
26 *
27 * @author <a href="mailto:ben.woodward@bbc.co.uk">Ben Woodward</a>
28 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
29 * @version $Id: ValidationHelper.java,v 1.4 2004/02/23 03:23:42 jford Exp $
30 */
31 public class ValidationHelper
32 {
33 //static string bigEmailRegex = new String("[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*(?:(?:[^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff]+(?![^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff])|\"[^//////x80-//xff//n//015\"]*(?:////[^//x80-//xff][^//////x80-//xff//n//015\"]*)*\")[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*(?://.[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*(?:[^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff]+(?![^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff])|\"[^//////x80-//xff//n//015\"]*(?:////[^//x80-//xff][^//////x80-//xff//n//015\"]*)*\")[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*)*@[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*(?:[^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff]+(?![^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff])|//[(?:[^//////x80-//xff//n//015//[//]]|////[^//x80-//xff])*//])[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*(?://.[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*(?:[^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff]+(?![^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff])|//[(?:[^//////x80-//xff//n//015//[//]]|////[^//x80-//xff])*//])[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*)*|(?:[^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff]+(?![^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff])|\"[^//////x80-//xff//n//015\"]*(?:////[^//x80-//xff][^//////x80-//xff//n//015\"]*)*\")[^()<>@,;:\".//////[//]//x80-//xff//000-//010//012-//037]*(?:(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)|\"[^//////x80-//xff//n//015\"]*(?:////[^//x80-//xff][^//////x80-//xff//n//015\"]*)*\")[^()<>@,;:\".//////[//]//x80-//xff//000-//010//012-//037]*)*<[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*(?:@[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*(?:[^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff]+(?![^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff])|//[(?:[^//////x80-//xff//n//015//[//]]|////[^//x80-//xff])*//])[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*(?://.[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*(?:[^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff]+(?![^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff])|//[(?:[^//////x80-//xff//n//015//[//]]|////[^//x80-//xff])*//])[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*)*(?:,[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*@[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*(?:[^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff]+(?![^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff])|//[(?:[^//////x80-//xff//n//015//[//]]|////[^//x80-//xff])*//])[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*(?://.[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*(?:[^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff]+(?![^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff])|//[(?:[^//////x80-//xff//n//015//[//]]|////[^//x80-//xff])*//])[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*)*)*:[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*)?(?:[^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff]+(?![^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff])|\"[^//////x80-//xff//n//015\"]*(?:////[^//x80-//xff][^//////x80-//xff//n//015\"]*)*\")[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*(?://.[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*(?:[^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff]+(?![^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff])|\"[^//////x80-//xff//n//015\"]*(?:////[^//x80-//xff][^//////x80-//xff//n//015\"]*)*\")[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*)*@[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*(?:[^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff]+(?![^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff])|//[(?:[^//////x80-//xff//n//015//[//]]|////[^//x80-//xff])*//])[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*(?://.[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*//)[//040//t]*)*(?:[^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff]+(?![^(//040)<>@,;:\".//////[//]//000-//037//x80-//xff])|//[(?:[^//////x80-//xff//n//015//[//]]|////[^//x80-//xff])*//])[//040//t]*(?://([^//////x80-//xff//n//015()]*(?:(?:////[^//x80-//xff]|//([^//////x80-//xff//n//015()]*(?:////[^//x80-//xff][^//////x80-//xff//n//015()]*)*//))[^//////x80-//xff//n//015()]*)*\)[\040\t]*)*)*>)");
34 //static string emailRegex = new String("/^(([^//-][//w//-]*?//.?){1,})[^-](//@{1})([^//-][//w+//.//-])[^//2]{1,}[^//-]$/");
35 /***
36 * Tests that the input string contains only alpha numeric or white spaces
37 * <P>
38 * @param evalString The string that is to be evaluated
39 * @param required indicates whether the field is required or not
40 * @return True if the input is alpha numeric, false otherwise.
41 **/
42 public static boolean isAlphaNumeric(String evalString, boolean required)
43 {
44 if (StringUtils.isEmpty(evalString))
45 {
46 if (true == required)
47 {
48 return false;
49 }
50 return true;
51 }
52 Perl5Util util = new Perl5Util();
53 return util.match("/^[//w//s]+$/", evalString);
54 }
55
56 public static boolean isAlphaNumeric(String evalString, boolean required, int maxLength)
57 {
58 if (isTooLong(evalString, maxLength))
59 {
60 return false;
61 }
62 return isAlphaNumeric(evalString, required);
63 }
64
65 public static boolean isLooseAlphaNumeric(String evalString, boolean required)
66 {
67 if (StringUtils.isEmpty(evalString))
68 {
69 if (true == required)
70 {
71 return false;
72 }
73 return true;
74 }
75 Perl5Util util = new Perl5Util();
76 return util.match("/^[//w//s//.//,/////-//(//)//+]+$/", evalString);
77 }
78
79 public static boolean isLooseAlphaNumeric(String evalString, boolean required, int maxLength)
80 {
81 if (isTooLong(evalString, maxLength))
82 {
83 return false;
84 }
85 return isLooseAlphaNumeric(evalString, required);
86 }
87
88 /***
89 * Tests that the input string contains only numeric
90 * <P>
91 * @param evalString The string that is to be evaluated
92 * @return True if the input is numeric, false otherwise.
93 **/
94 public static boolean isDecimal(String evalString, boolean required)
95 {
96 if (StringUtils.isEmpty(evalString))
97 {
98 if (true == required)
99 {
100 return false;
101 }
102 return true;
103 }
104 Perl5Util util = new Perl5Util();
105 return util.match("/^(//d+//.)?//d+$/", evalString);
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 Perl5Util util = new Perl5Util();
134 return util.match("/^//d+$/", evalString);
135 }
136
137 public static boolean isInteger(String evalString, boolean required, int maxLength)
138 {
139 if (isTooLong(evalString, maxLength))
140 {
141 return false;
142 }
143 return isInteger(evalString, required);
144 }
145
146 /***
147 * Tests that the input string contains a valid email addess
148 * <P>
149 * @param evalString The string that is to be evaluated
150 * @return True if the input is a valid email address.
151 **/
152 public static boolean isEmailAddress(String evalString, boolean required)
153 {
154 if (StringUtils.isEmpty(evalString))
155 {
156 if (true == required)
157 {
158 return false;
159 }
160 return true;
161 }
162 Perl5Util util = new Perl5Util();
163 return util.match("/^(?://w[//w-]*//.)*//w[//w-]*@(?://w[//w-]*//.)+//w[//w-]*$/", evalString);
164 }
165
166 public static boolean isEmailAddress(String evalString, boolean required, int maxLength)
167 {
168 if (isTooLong(evalString, maxLength))
169 {
170 return false;
171 }
172 return isEmailAddress(evalString, required);
173 }
174
175 /***
176 * Tests that the input string contains a valid URL
177 * <P>
178 * @param evalString The string that is to be evaluated
179 * @return True if the input is a valid URL.
180 **/
181 public static boolean isURL(String evalString, boolean required)
182 {
183 try
184 {
185 if (StringUtils.isEmpty(evalString))
186 {
187 if (true == required)
188 {
189 return false;
190 }
191 return true;
192 }
193
194 URL url = new URL(evalString);
195
196 /*
197 Perl5Util util = new Perl5Util();
198 System.out.println("looking at " +evalString);
199 return util.match("/^[//w%?-_~]$/", evalString);
200 */
201 //Object content = url.getContent();
202 //System.out.println("url contains :["+content+"]");
203 return true;
204 }
205 catch (Exception e)
206 {
207 System.err.println(evalString+" is not a valid URL: "+ e);
208 return false;
209 }
210 }
211
212 public static boolean isURL(String evalString, boolean required, int maxLength)
213 {
214 if (isTooLong(evalString, maxLength))
215 {
216 return false;
217 }
218 return isURL(evalString, required);
219 }
220
221 public static boolean isValidIdentifier(String folderName)
222 {
223 boolean valid = true;
224
225 char[] chars = folderName.toCharArray();
226 for (int ix = 0; ix < chars.length; ix++)
227 {
228 if (!Character.isJavaIdentifierPart(chars[ix]))
229 {
230 valid = false;
231 break;
232 }
233 }
234 return valid;
235 }
236
237 public static boolean isTooLong(String evalString, int maxLength)
238 {
239 if (null != evalString)
240 {
241 return (evalString.length() > maxLength);
242 }
243 return false;
244 }
245
246 }
247