Package org.apache.myfaces.custom.urlvalidator

Source Code of org.apache.myfaces.custom.urlvalidator.UrlValidator

package org.apache.myfaces.custom.urlvalidator;

import org.apache.myfaces.util.MessageUtils;

import org.apache.commons.validator.GenericValidator;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

public class UrlValidator implements Validator {

  /**
   * <p>The standard converter id for this converter.</p>
   */
  public static final String   VALIDATOR_ID      = "org.apache.myfaces.validator.Url";
  /**
   * <p>The message identifier of the {@link FacesMessage} to be created if
   * the maximum length check fails.</p>
   */
  public static final String URL_MESSAGE_ID = "org.apache.myfaces.Url.INVALID";

  public UrlValidator(){
  }

  /**
   * method that validates an url address.
   * it uses the commons-validator
   */
  public void validate(
    FacesContext facesContext,
    UIComponent uiComponent,
    Object value)
    throws ValidatorException {


      if (facesContext == null) throw new NullPointerException("facesContext");
      if (uiComponent == null) throw new NullPointerException("uiComponent");

      if (value == null)
      {
        return;
      }
      if (!GenericValidator.isUrl(value.toString())) {
        Object[] args = {value.toString()};
        throw new ValidatorException(MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR,URL_MESSAGE_ID, args));

      }

  }

}
TOP

Related Classes of org.apache.myfaces.custom.urlvalidator.UrlValidator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.