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

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

Introduction

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

Prototype

public Integer validate(String value) 

Source Link

Document

Validate/convert an Integer using the default Locale.

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);

    /**//  w  ww  . j a v  a2  s . c om
     * 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:com.btobits.automator.ant.sql.task.SQLCompareTask.java

private int getColumnType(final SQLCompareTask.VerifyCell inRowPos) throws Exception {
    final IntegerValidator validator = new IntegerValidator();
    if (validator.isValid(inRowPos.getField())) {
        Integer id = validator.validate(inRowPos.getField());
        return resultSet.getMetaData().getColumnType(id);
    } else {/*from  www.  ja va 2  s  .  c  om*/
        for (int i = 0; i < resultSet.getMetaData().getColumnCount(); i++) {
            final String nameColumn = resultSet.getMetaData().getColumnName(i + 1);
            if (StringUtils.equalsIgnoreCase(nameColumn, inRowPos.getField())) {
                return resultSet.getMetaData().getColumnType(i + 1);
            }
        }
    }
    throw new Exception("Unknown field name [" + inRowPos.getField() + "].");
}