001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.uilib.util;
016:
017: import java.text.ParsePosition;
018: import java.text.SimpleDateFormat;
019: import java.util.Date;
020: import java.util.StringTokenizer;
021: import org.apache.commons.validator.GenericValidator;
022:
023: /**
024: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
025: */
026: public class ValidationUtil {
027:
028: /**
029: * Tries to parse the date according to the given patterns. The patterns correspond to the usual
030: * {@link SimpleDateFormat} patterns with one addition: one can combine them using "|" so that
031: * if at least one pattern parses the input it will be used.
032: *
033: * @param dateTimeString date to be parsed.
034: * @param format {@link SimpleDateFormat} patterns with "|".
035: * @return parsed {@link Date} or null if parsing fails.
036: */
037: public static ParsedDate parseDate(String dateTimeString,
038: String format) {
039: ParsedDate result = null;
040: StringTokenizer tokenizer = new StringTokenizer(format, "|");
041:
042: String[] patterns = new String[tokenizer.countTokens()];
043: for (int i = 0; tokenizer.hasMoreTokens(); i++)
044: patterns[i] = tokenizer.nextToken();
045:
046: for (int i = 0; i < patterns.length; i++) {
047: Date date = null;
048:
049: SimpleDateFormat dateFormat = new SimpleDateFormat(
050: patterns[i]);
051: dateFormat.setLenient(false);
052: ParsePosition pos = new ParsePosition(0);
053:
054: date = dateFormat.parse(dateTimeString, pos);
055:
056: if (date != null
057: && (pos.getIndex() == dateTimeString.length()))
058: result = new ParsedDate(date, patterns[i]);
059:
060: // introduce the y10k problem && ignore everything B.C
061: // needed to escape SimpleDateFormats broken guesswork that can produce corrupt Dates.
062: if (result != null) {
063: int year = result.getDate().getYear();
064: if (year > 9999 || year < 0)
065: result = null;
066:
067: /* checking just year is not enough b/c some strings like "020110999" "02.01.11500"
068: still manage to pass through whereas others with same pattern do not - for example
069: "02.01.13452". Guess it is all about the zeroes. */
070:
071: // so, check the length too. Means that dd.MM.yyyy does not interpret d.M.yyyy, unless
072: // format parameter contains it.
073: if (dateTimeString.trim().length() != patterns[i]
074: .length())
075: result = null;
076: }
077:
078: if (result != null)
079: break;
080: }
081:
082: return result;
083: }
084:
085: public static class ParsedDate {
086: protected Date date;
087: protected String outputPattern;
088:
089: public ParsedDate(Date date, String outputPattern) {
090: this .date = date;
091: this .outputPattern = outputPattern;
092: }
093:
094: public Date getDate() {
095: return this .date;
096: }
097:
098: public String getOutputPattern() {
099: return this .outputPattern;
100: }
101: }
102:
103: /**
104: * Checks whether the string is a valid email.
105: *
106: * @param emailString supposed email string.
107: * @return whether the string is a valid email.
108: */
109: public static boolean isEmail(String emailString) {
110: return GenericValidator.isEmail(emailString);
111: }
112: }
|