PageRenderTime 21ms CodeModel.GetById 16ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/webportal/src/main/java/au/org/emii/portal/util/Validate.java

http://alageospatialportal.googlecode.com/
Java | 105 lines | 45 code | 14 blank | 46 comment | 4 complexity | 4235ae25a0844869be67d49ed62225f8 MD5 | raw file
  1package au.org.emii.portal.util;
  2
  3import java.text.DateFormat;
  4import java.text.SimpleDateFormat;
  5
  6import org.apache.commons.lang.StringEscapeUtils;
  7import org.apache.commons.validator.EmailValidator;
  8import org.apache.commons.validator.UrlValidator;
  9
 10/**
 11 * Quick and easy wrapper to do common escaping and validation routines
 12 * @author geoff
 13 *
 14 */
 15public class Validate {
 16	
 17	/**
 18	 * Check if a passed in string is empty  
 19	 * @param string
 20	 * @return false if String is not null, empty or just whitespace, otherwise
 21	 * return true
 22	 */
 23	public static boolean empty(String string) {
 24		return (!((string != null) && (! string.matches("\\s*"))));
 25	}
 26	
 27	/**
 28	 * Escape HTML using StringEscapeUtils and then trim any whitespace 
 29	 * from start and end using String class
 30	 */
 31	public static String escapeHtmlAndTrim(String string) {
 32		return StringEscapeUtils.escapeHtml(string).trim();
 33	}
 34	
 35	/**
 36	 * Check a passed in string is NOT a valid http[s] uri.  Remove
 37	 * any whitespace for the test
 38	 * @param uri to be checked
 39	 * @return true if the uri is invalid, otherwise false
 40	 */
 41	public static boolean invalidHttpUri(String uri) {
 42		UrlValidator urlValidator = new UrlValidator(
 43				new String[] { "http","https" }
 44		);
 45		
 46		return (!urlValidator.isValid(uri.trim()));
 47
 48	}
 49	
 50	/**
 51	 * Check if a uri stars with http[s]://, if it doesn't, prepend
 52	 * it and return the new string
 53	 * @param string
 54	 * @return
 55	 */
 56	public static String prefixUri(String uri) {
 57		/* idiot proofing: prefix with "http://" if we dont' start
 58		 * with either http:// or https://
 59		 */
 60		String fullUri;
 61		if (! uri.matches("^[Hh][Tt][Tt][Pp][Ss]?://.*")) {
 62			fullUri = "http://" + uri;
 63		}
 64		else {
 65			fullUri = uri;
 66		}
 67		
 68		return fullUri;
 69	}
 70	
 71	/**
 72	 * Return a DateFormat instance configured for long ISO 8601
 73	 * dates (eg 2004-05-04T23:50:03.045Z)  
 74	 * @return
 75	 */
 76	public static DateFormat getIsoDateFormatter() {
 77		SimpleDateFormat sd = new SimpleDateFormat(
 78				"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
 79		);
 80		return sd;
 81	}
 82	
 83	/**
 84	 * Return a DateFormat instance configured for short ISO 8601
 85	 * dates (eg 2004-05-04)  
 86	 * @return
 87	 */
 88	public static SimpleDateFormat getShortIsoDateFormatter() {
 89		SimpleDateFormat sd = new SimpleDateFormat(
 90				"yyyy-MM-dd"
 91		);
 92		return sd;
 93	}
 94
 95    /**
 96     * Validate an email address - return true for valid, false for invalid.
 97     * Wraps commons Emailvalidator 
 98     * @param email
 99     * @return
100     */
101    public static boolean email(String email) {
102        return EmailValidator.getInstance().isValid(email);
103    }
104
105}