Example usage for org.apache.commons.validator GenericValidator isUrl

List of usage examples for org.apache.commons.validator GenericValidator isUrl

Introduction

In this page you can find the example usage for org.apache.commons.validator GenericValidator isUrl.

Prototype

public static boolean isUrl(String value) 

Source Link

Document

Checks if a field is a valid url address.

If you need to modify what is considered valid then consider using the UrlValidator directly.

Usage

From source file:jp.terasoluna.fw.validation.ValidationUtil.java

/**
 * ?URL??????????//from   w w w  . ja v  a  2s  .  c o m
 *
 * <code>null</code> ???????
 *
 * @param value 
 * @param allowallschemes ????????
 * @param allow2slashes ??????
 * @param nofragments URL??????
 * @param schemesVar ??
 * ?????
 * @return
 *            ?URL??????
 *            <code>true</code>?
 *            ?????<code>false</code>?
 */
public static boolean isUrl(String value, boolean allowallschemes, boolean allow2slashes, boolean nofragments,
        String schemesVar) {

    if (StringUtils.isEmpty(value)) {
        return true;
    }

    // ?
    int options = 0;
    if (allowallschemes) {
        options += UrlValidator.ALLOW_ALL_SCHEMES;
    }
    if (allow2slashes) {
        options += UrlValidator.ALLOW_2_SLASHES;
    }
    if (nofragments) {
        options += UrlValidator.NO_FRAGMENTS;
    }

    // ??????GenericValidator
    if (options == 0 && schemesVar == null) {
        if (GenericValidator.isUrl(value)) {
            return true;
        }
        return false;
    }

    // String[]??
    String[] schemes = null;
    if (schemesVar != null) {

        StringTokenizer st = new StringTokenizer(schemesVar, ",");
        schemes = new String[st.countTokens()];

        int i = 0;
        while (st.hasMoreTokens()) {
            schemes[i++] = st.nextToken().trim();
        }
    }

    // ????UrlValidator
    UrlValidator urlValidator = new UrlValidator(schemes, options);
    if (urlValidator.isValid(value)) {
        return true;
    }
    return false;
}

From source file:org.apache.struts.validator.FieldChecks.java

/**
 * Checks if a field has a valid url. Four optional variables can be
 * specified to configure url validation.
 *
 * <ul>/*  w w w .ja v  a 2  s.c  o  m*/
 *
 * <li>Variable <code>allow2slashes</code> can be set to <code>true</code>
 * or <code>false</code> to control whether two slashes are allowed -
 * default is <code>false</code> (i.e. two slashes are NOT allowed).</li>
 *
 * <li>Variable <code>nofragments</code> can be set to <code>true</code>
 * or <code>false</code> to control whether fragments are allowed -
 * default is <code>false</code> (i.e. fragments ARE allowed).</li>
 *
 * <li>Variable <code>allowallschemes</code> can be set to
 * <code>true</code> or <code>false</code> to control if all schemes are
 * allowed - default is <code>false</code> (i.e. all schemes are NOT
 * allowed).</li>
 *
 * <li>Variable <code>schemes</code> can be set to a comma delimited list
 * of valid schemes. This value is ignored if <code>allowallschemes</code>
 * is set to <code>true</code>. Default schemes allowed are "http",
 * "https" and "ftp" if this variable is not specified.</li>
 *
 * </ul>
 *
 * @param bean      The bean validation is being performed on.
 * @param va        The <code>ValidatorAction</code> that is currently
 *                  being performed.
 * @param field     The <code>Field</code> object associated with the
 *                  current field being validated.
 * @param errors    The <code>ActionMessages</code> object to add errors
 *                  to if any validation errors occur.
 * @param validator The <code>Validator</code> instance, used to access
 *                  other field values.
 * @param request   Current request object.
 * @return True if valid, false otherwise.
 */
public static boolean validateUrl(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {
    String value = null;

    value = evaluateBean(bean, field);

    if (GenericValidator.isBlankOrNull(value)) {
        return true;
    }

    // Get the options and schemes Vars
    String allowallschemesVar = Resources.getVarValue("allowallschemes", field, validator, request, false);
    boolean allowallschemes = "true".equalsIgnoreCase(allowallschemesVar);
    int options = allowallschemes ? UrlValidator.ALLOW_ALL_SCHEMES : 0;

    String allow2slashesVar = Resources.getVarValue("allow2slashes", field, validator, request, false);

    if ("true".equalsIgnoreCase(allow2slashesVar)) {
        options += UrlValidator.ALLOW_2_SLASHES;
    }

    String nofragmentsVar = Resources.getVarValue("nofragments", field, validator, request, false);

    if ("true".equalsIgnoreCase(nofragmentsVar)) {
        options += UrlValidator.NO_FRAGMENTS;
    }

    String schemesVar = allowallschemes ? null
            : Resources.getVarValue("schemes", field, validator, request, false);

    // No options or schemes - use GenericValidator as default
    if ((options == 0) && (schemesVar == null)) {
        if (GenericValidator.isUrl(value)) {
            return true;
        } else {
            errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));

            return false;
        }
    }

    // Parse comma delimited list of schemes into a String[]
    String[] schemes = null;

    if (schemesVar != null) {
        StringTokenizer st = new StringTokenizer(schemesVar, ",");

        schemes = new String[st.countTokens()];

        int i = 0;

        while (st.hasMoreTokens()) {
            schemes[i++] = st.nextToken().trim();
        }
    }

    // Create UrlValidator and validate with options/schemes
    UrlValidator urlValidator = new UrlValidator(schemes, options);

    if (urlValidator.isValid(value)) {
        return true;
    } else {
        errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));

        return false;
    }
}

From source file:org.asqatasun.rules.elementchecker.lang.LangChecker.java

/**
 * /*ww  w.j a  v a  2s. com*/
 * @param extractedText
 * @return 
 */
protected boolean isTextTestable(String extractedText) {
    if (StringUtils.isBlank(extractedText)) {
        return false;
    }
    String textToTest = StringUtils.trim(extractedText);
    Matcher m = nonAlphanumericPattern.matcher(textToTest);
    return !m.matches() && !GenericValidator.isEmail(textToTest) && !GenericValidator.isUrl(textToTest);
}