001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.portals.gems.util;
018:
019: import java.text.ParseException;
020: import java.text.SimpleDateFormat;
021: import java.util.Date;
022:
023: import org.apache.commons.lang.StringUtils;
024:
025: /**
026: * ValidationHelper using regular expressions
027: *
028: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
029: * @version $Id: $
030: */
031: public abstract class ValidationHelper {
032: public static final SimpleDateFormat EUROPEAN_DATE_FORMAT = new SimpleDateFormat(
033: "dd-MM-yyyy");
034: public static final SimpleDateFormat EUROPEAN_DATETIME_FORMAT = new SimpleDateFormat(
035: "dd-MM-yyyy HH:mm");
036: public static final SimpleDateFormat AMERICAN_DATE_FORMAT = new SimpleDateFormat(
037: "MM-dd-yyyy");
038: public static final SimpleDateFormat AMERICAN_DATETIME_FORMAT = new SimpleDateFormat(
039: "MM-dd-yyyy HH:mm");
040:
041: /**
042: * Tests that the input string contains only alpha numeric or white spaces
043: * <P>
044: * @param evalString The string that is to be evaluated
045: * @param required indicates whether the field is required or not
046: * @return True if the input is alpha numeric, false otherwise.
047: **/
048: public static boolean isAlphaNumeric(String evalString,
049: boolean required) {
050: if (StringUtils.isEmpty(evalString)) {
051: if (true == required) {
052: return false;
053: }
054: return true;
055: }
056: return evalString.matches("^[\\w\\s]+$");
057: }
058:
059: public static boolean isAlphaNumeric(String evalString,
060: boolean required, int maxLength) {
061: if (isTooLong(evalString, maxLength)) {
062: return false;
063: }
064: return isAlphaNumeric(evalString, required);
065: }
066:
067: public static boolean isLooseAlphaNumeric(String evalString,
068: boolean required) {
069: if (StringUtils.isEmpty(evalString)) {
070: if (true == required) {
071: return false;
072: }
073: return true;
074: }
075: return evalString.matches("^[\\w\\s\\.\\,\\/\\-\\(\\)\\+]+$");
076: }
077:
078: public static boolean isLooseAlphaNumeric(String evalString,
079: boolean required, int maxLength) {
080: if (isTooLong(evalString, maxLength)) {
081: return false;
082: }
083: return isLooseAlphaNumeric(evalString, required);
084: }
085:
086: /**
087: * Tests that the input string contains only numeric
088: * <P>
089: * @param evalString The string that is to be evaluated
090: * @return True if the input is numeric, false otherwise.
091: **/
092: public static boolean isDecimal(String evalString, boolean required) {
093: if (StringUtils.isEmpty(evalString)) {
094: if (true == required) {
095: return false;
096: }
097: return true;
098: }
099: return evalString.matches("^(\\d+\\.)?\\d+$");
100: }
101:
102: public static boolean isDecimal(String evalString,
103: boolean required, int maxLength) {
104: if (isTooLong(evalString, maxLength)) {
105: return false;
106: }
107: return isDecimal(evalString, required);
108: }
109:
110: /**
111: * Tests that the input string contains only an integer
112: * <P>
113: * @param evalString The string that is to be evaluated
114: * @return True if the input is numeric, false otherwise.
115: **/
116: public static boolean isInteger(String evalString, boolean required) {
117: if (StringUtils.isEmpty(evalString)) {
118: if (true == required) {
119: return false;
120: }
121: return true;
122: }
123: return evalString.matches("^\\d+$");
124: }
125:
126: public static boolean isInteger(String evalString,
127: boolean required, int maxLength) {
128: if (isTooLong(evalString, maxLength)) {
129: return false;
130: }
131: return isInteger(evalString, required);
132: }
133:
134: /**
135: * Tests that the input string contains a valid email addess
136: * <P>
137: * @param evalString The string that is to be evaluated
138: * @return True if the input is a valid email address.
139: **/
140: public static boolean isEmailAddress(String evalString,
141: boolean required) {
142: if (StringUtils.isEmpty(evalString)) {
143: if (true == required) {
144: return false;
145: }
146: return true;
147: }
148: return evalString
149: .matches("^(?:\\w[\\w-]*\\.)*\\w[\\w-]*@(?:\\w[\\w-]*\\.)+\\w[\\w-]*$");
150: }
151:
152: public static boolean isEmailAddress(String evalString,
153: boolean required, int maxLength) {
154: if (isTooLong(evalString, maxLength)) {
155: return false;
156: }
157: return isEmailAddress(evalString, required);
158: }
159:
160: /**
161: * Tests that the input string contains a valid URL
162: * <P>
163: * @param evalString The string that is to be evaluated
164: * @return True if the input is a valid URL.
165: **/
166: public static boolean isURL(String evalString, boolean required) {
167: try {
168: if (StringUtils.isEmpty(evalString)) {
169: if (true == required) {
170: return false;
171: }
172: return true;
173: }
174:
175: //URL url = new URL(evalString);
176:
177: /*
178: Perl5Util util = new Perl5Util();
179: System.out.println("looking at " +evalString);
180: return evalString.matches("^[\\w%?-_~]$", evalString);
181: */
182: //Object content = url.getContent();
183: //System.out.println("url contains :["+content+"]");
184: return true;
185: } catch (Exception e) {
186: System.err
187: .println(evalString + " is not a valid URL: " + e);
188: return false;
189: }
190: }
191:
192: public static boolean isURL(String evalString, boolean required,
193: int maxLength) {
194: if (isTooLong(evalString, maxLength)) {
195: return false;
196: }
197: return isURL(evalString, required);
198: }
199:
200: public static boolean isValidIdentifier(String folderName) {
201: boolean valid = true;
202:
203: char[] chars = folderName.toCharArray();
204: for (int ix = 0; ix < chars.length; ix++) {
205: if (!Character.isJavaIdentifierPart(chars[ix])) {
206: valid = false;
207: break;
208: }
209: }
210: return valid;
211: }
212:
213: public static boolean isTooLong(String evalString, int maxLength) {
214: if (null != evalString) {
215: return (evalString.length() > maxLength);
216: }
217: return false;
218: }
219:
220: public static boolean isPhoneNumber(String evalString,
221: boolean required, int maxLength) {
222: if (isTooLong(evalString, maxLength)) {
223: return false;
224: }
225: return isPhoneNumber(evalString, required);
226: }
227:
228: public static boolean isPhoneNumber(String evalString,
229: boolean required) {
230: if (StringUtils.isEmpty(evalString)) {
231: if (true == required) {
232: return false;
233: }
234: return true;
235: }
236: //return evalString.matches("[(][0-9]{3}[)][ ]*[0-9]{3}-[0-9]{4}", evalString);
237: return evalString
238: .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}");
239: }
240:
241: public static Date parseDate(String formatted) {
242: Date date = null;
243: if (null == formatted) {
244: return null;
245: }
246: try {
247: synchronized (EUROPEAN_DATE_FORMAT) {
248: date = EUROPEAN_DATE_FORMAT.parse(formatted);
249: }
250: } catch (ParseException e) {
251: try {
252: synchronized (AMERICAN_DATE_FORMAT) {
253: date = AMERICAN_DATE_FORMAT.parse(formatted);
254: }
255: } catch (ParseException ee) {
256: }
257: }
258: return date;
259: }
260:
261: public static Date parseDatetime(String formatted) {
262: Date date = null;
263: if (null == formatted) {
264: return null;
265: }
266:
267: try {
268: synchronized (EUROPEAN_DATETIME_FORMAT) {
269: date = EUROPEAN_DATETIME_FORMAT.parse(formatted);
270: }
271: } catch (ParseException e) {
272: try {
273: synchronized (AMERICAN_DATETIME_FORMAT) {
274: date = AMERICAN_DATETIME_FORMAT.parse(formatted);
275: }
276: } catch (ParseException ee) {
277: }
278: }
279: return date;
280: }
281:
282: public static String formatEuropeanDate(Date date) {
283: if (null == date) {
284: return null;
285: }
286: synchronized (EUROPEAN_DATE_FORMAT) {
287: return EUROPEAN_DATE_FORMAT.format(date);
288: }
289: }
290:
291: public static String formatAmericanDate(Date date) {
292: if (null == date) {
293: return null;
294: }
295: synchronized (AMERICAN_DATE_FORMAT) {
296: return AMERICAN_DATE_FORMAT.format(date);
297: }
298: }
299:
300: public static String formatEuropeanDatetime(Date date) {
301: if (null == date) {
302: return null;
303: }
304: synchronized (EUROPEAN_DATETIME_FORMAT) {
305: return EUROPEAN_DATETIME_FORMAT.format(date);
306: }
307: }
308:
309: public static String formatAmericanDatetime(Date date) {
310: if (null == date) {
311: return null;
312: }
313: synchronized (AMERICAN_DATETIME_FORMAT) {
314: return AMERICAN_DATETIME_FORMAT.format(date);
315: }
316: }
317:
318: public static boolean isValidDate(String formatted) {
319: if (formatted == null || formatted.trim().length() == 0)
320: return true;
321:
322: try {
323: EUROPEAN_DATE_FORMAT.parse(formatted);
324: } catch (ParseException e) {
325: try {
326: synchronized (AMERICAN_DATE_FORMAT) {
327: AMERICAN_DATE_FORMAT.parse(formatted);
328: }
329: } catch (ParseException ee) {
330: return false;
331: }
332: }
333: return true;
334: }
335:
336: public static boolean isValidDatetime(String formatted) {
337: if (formatted == null || formatted.trim().length() == 0)
338: return true;
339:
340: try {
341: synchronized (EUROPEAN_DATETIME_FORMAT) {
342: EUROPEAN_DATETIME_FORMAT.parse(formatted);
343: }
344: } catch (ParseException e) {
345: try {
346: synchronized (AMERICAN_DATETIME_FORMAT) {
347: AMERICAN_DATETIME_FORMAT.parse(formatted);
348: }
349: } catch (ParseException ee) {
350: return false;
351: }
352: }
353: return true;
354: }
355:
356: public static boolean isAny(String evalString, boolean required) {
357: if (StringUtils.isEmpty(evalString)) {
358: if (true == required) {
359: return false;
360: }
361: return true;
362: }
363: return true;
364: }
365:
366: public static boolean isAny(String evalString, boolean required,
367: int maxLength) {
368: if (isTooLong(evalString, maxLength)) {
369: return false;
370: }
371: return isAny(evalString, required);
372: }
373:
374: }
|