Example usage for org.apache.commons.validator.routines IntegerValidator minValue

List of usage examples for org.apache.commons.validator.routines IntegerValidator minValue

Introduction

In this page you can find the example usage for org.apache.commons.validator.routines IntegerValidator minValue.

Prototype

public boolean minValue(Integer value, int min) 

Source Link

Document

Check if the value is greater than or equal to a minimum.

Usage

From source file:au.edu.anu.portal.portlets.rss.validator.SimpleRSSPreferencesValidator.java

@Override
public void validate(PortletPreferences prefs) throws ValidatorException {

    //get prefs as strings
    String max_items = prefs.getValue("max_items", Integer.toString(Constants.MAX_ITEMS));
    String feed_url = prefs.getValue(PREF_FEED_URL, null);

    //check readonly
    boolean feedUrlIsLocked = prefs.isReadOnly(PREF_FEED_URL);

    /**/*from  w ww  .  j  a  v a  2 s . c o  m*/
     * max_items
     */
    IntegerValidator integerValidator = IntegerValidator.getInstance();
    Integer maxItems = integerValidator.validate(max_items);

    //check it's a number
    if (maxItems == null) {
        throw new ValidatorException("Invalid value, must be a number", Collections.singleton("max_items"));
    }

    //check greater than or equal to a minimum of 1
    if (!integerValidator.minValue(maxItems, Constants.MIN_ITEMS)) {
        throw new ValidatorException("Invalid number, must be greater than 0",
                Collections.singleton("max_items"));
    }

    /**
     * feed_url
     */
    //only validate if it's not readonly
    if (!feedUrlIsLocked) {
        String[] schemes = { "http", "https" };
        DetailedUrlValidator urlValidator = new DetailedUrlValidator(schemes);

        //check not null
        if (StringUtils.isBlank(feed_url)) {
            throw new ValidatorException("You must specify a URL for the RSS feed",
                    Collections.singleton(PREF_FEED_URL));
        }

        //check valid scheme
        if (!urlValidator.isValidScheme(feed_url)) {
            throw new ValidatorException("Invalid feed scheme. Must be one of: " + Arrays.toString(schemes),
                    Collections.singleton(PREF_FEED_URL));
        }

        //check valid URL
        if (!urlValidator.isValid(feed_url)) {
            throw new ValidatorException("Invalid feed URL", Collections.singleton(PREF_FEED_URL));

        }
    }

    /**
     * portlet_title not validated here as it is reasonable to allow blank entry. We deal with this later
     */

}

From source file:org.mule.modules.validation.ValidationModule.java

/**
 * If the specified <code>value</code> is not a valid {@link Integer} throw an exception.
 * <p/>//from   www .  j av a  2s.  co m
 * {@sample.xml ../../../doc/mule-module-validation.xml.sample validation:validate-integer}
 *
 * @param value                    Value to validate
 * @param locale                   The locale to use for the format
 * @param pattern                  The pattern used to format the value
 * @param minValue                 The minimum value
 * @param maxValue                 The maximum value
 * @param customExceptionClassName Class name of the exception to throw
 * @throws Exception if not valid
 */
@Processor
public void validateInteger(String value, @Optional @Default("US") Locale locale, @Optional String pattern,
        @Optional Integer minValue, @Optional Integer maxValue,
        @Optional @Default("org.mule.modules.validation.InvalidException") String customExceptionClassName)
        throws Exception {
    IntegerValidator validator = IntegerValidator.getInstance();

    Integer newValue = null;
    if (pattern != null) {
        newValue = validator.validate(value, pattern, locale.getJavaLocale());
    } else {
        newValue = validator.validate(value, locale.getJavaLocale());
    }

    if (newValue == null) {
        throw buildException(customExceptionClassName);
    }
    if (minValue != null) {
        if (!validator.minValue(newValue, minValue)) {
            throw buildException(customExceptionClassName);
        }
    }
    if (maxValue != null) {
        if (!validator.maxValue(newValue, maxValue)) {
            throw buildException(customExceptionClassName);
        }
    }
}