Example usage for org.apache.commons.lang3 StringUtils isAlphanumericSpace

List of usage examples for org.apache.commons.lang3 StringUtils isAlphanumericSpace

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils isAlphanumericSpace.

Prototype

public static boolean isAlphanumericSpace(final CharSequence cs) 

Source Link

Document

Checks if the CharSequence contains only Unicode letters, digits or space ( ' ' ).

null will return false .

Usage

From source file:com.github.britter.beanvalidators.strings.AlphaNumericConstraintValidator.java

@Override
public boolean isValid(final String value, final ConstraintValidatorContext context) {
    // Don't validate null, empty and blank strings, since these are validated by @NotNull, @NotEmpty and @NotBlank
    return StringUtils.isBlank(value) || allowSpaces ? StringUtils.isAlphanumericSpace(value)
            : StringUtils.isAlphanumeric(value);
}

From source file:com.kynomics.control.HalterController.java

public void validateAlphaNumeric(FacesContext context, UIComponent uiComponent, Object value)
        throws ValidatorException {
    if (!StringUtils.isAlphanumericSpace((String) value)) {
        HtmlInputText htmlInputText = (HtmlInputText) uiComponent;
        FacesMessage facesMessage = new FacesMessage(
                htmlInputText.getLabel() + ": nur Buchstaben und Zahlen erlaubt");
        throw new ValidatorException(facesMessage);
    }//from   w  w w .  j  ava 2 s .  c om
}

From source file:org.jaqpot.core.service.validator.ParameterValidator.java

public void validate(String input, Set<Parameter> parameters)
        throws ParameterTypeException, ParameterRangeException, ParameterScopeException {

    Map<String, Object> parameterMap = null;
    if (input != null) {
        parameterMap = serializer.parse(input, new HashMap<String, Object>().getClass());
    }/*from  w  w w .  j  av  a 2s .  c  o m*/

    //For each mandatory parameter in stored algorithm, check if it exists in user input
    if (parameters != null) {
        for (Parameter parameter : parameters) {
            if (parameter.getScope().equals(Parameter.Scope.MANDATORY)
                    && (parameterMap == null || !parameterMap.containsKey(parameter.getId()))) {
                throw new ParameterScopeException(
                        "Parameter with id: '" + parameter.getId() + "' is mandatory.");
            }
        }
    }

    //For each parameter in set
    if (parameterMap != null && parameters != null) {
        for (Map.Entry<String, Object> entry : parameterMap.entrySet()) {
            String parameterId = entry.getKey();

            Parameter parameter = parameters.stream().filter(p -> p.getId().equals(parameterId)).findFirst()
                    .orElseThrow(() -> new ParameterTypeException(
                            "Could not recognise parameter with id:" + parameterId));
            Object value = entry.getValue();

            //Get type of algorithm's parameter
            Type typeOfParameter = Type.UNDEFINED;
            if (isNumeric(parameter.getValue().toString())) {
                typeOfParameter = Type.NUMERIC;
            } else if (StringUtils.isAlphanumericSpace(parameter.getValue().toString())) {
                typeOfParameter = Type.STRING;
            } else if (parameter.getValue() instanceof Collection) {
                typeOfParameter = getTypeOfCollection((Collection) parameter.getValue());
            }

            if (typeOfParameter == Type.UNDEFINED) {
                continue; //parameter is of a type that we cannot validate.
            }
            //Get type of user's value and validate
            switch (typeOfParameter) {
            case NUMERIC:
                if (isNumeric(value.toString())) {
                    if (parameter.getAllowedValues() != null) {
                        checkAllowedValues(parameterId, value, parameter.getAllowedValues());
                    }
                    if (parameter.getMinValue() != null && isNumeric(parameter.getMinValue().toString())) {
                        checkIsLessThan(parameterId, Double.parseDouble(value.toString()),
                                (Double.parseDouble(parameter.getMinValue().toString())));
                    }
                    if (parameter.getMaxValue() != null && isNumeric(parameter.getMaxValue().toString())) {
                        checkIsGreaterThan(parameterId, Double.parseDouble(value.toString()),
                                (Double.parseDouble(parameter.getMaxValue().toString())));
                    }
                } else {
                    throw new ParameterTypeException("Parameter with id:" + parameterId + " must be Numeric");
                }
                break;
            case STRING:
                if (StringUtils.isAlphanumericSpace(value.toString())) {
                    if (parameter.getAllowedValues() != null) {
                        checkAllowedValues(parameterId, value.toString(), parameter.getAllowedValues());
                    }
                    if (parameter.getMinValue() != null && isNumeric(parameter.getMinValue().toString())) {
                        checkIsLessThan(parameterId, value.toString(), parameter.getMinValue().toString());
                    }
                    if (parameter.getMaxValue() != null && isNumeric(parameter.getMaxValue().toString())) {
                        checkIsGreaterThan(parameterId, value.toString(), parameter.getMaxValue().toString());
                    }
                } else {
                    throw new ParameterTypeException(
                            "Parameter with id: '" + parameterId + "' must be Alphanumeric");
                }
                break;
            case NUMERIC_ARRAY:
                if ((value instanceof Collection
                        && getTypeOfCollection((Collection) value) == Type.NUMERIC_ARRAY)) {
                    if (parameter.getAllowedValues() != null) {
                        checkAllowedValues(parameterId, value, parameter.getAllowedValues());
                    }
                    checkMinMaxSize(parameterId, (Collection) value, parameter.getMinArraySize(),
                            parameter.getMaxArraySize());
                    for (Object o : (Collection) value) {
                        if (parameter.getMinValue() != null && isNumeric(parameter.getMinValue().toString())) {
                            checkIsLessThan(parameterId, Double.parseDouble(o.toString()),
                                    (Double.parseDouble(parameter.getMinValue().toString())));
                        }
                        if (parameter.getMaxValue() != null && isNumeric(parameter.getMaxValue().toString())) {
                            checkIsGreaterThan(parameterId, Double.parseDouble(o.toString()),
                                    (Double.parseDouble(parameter.getMaxValue().toString())));
                        }
                    }
                } else {
                    throw new ParameterTypeException(
                            "Parameter with id: '" + parameterId + "' must be Array of numeric values");
                }
                break;
            case STRING_ARRAY:
                if ((value instanceof Collection
                        && getTypeOfCollection((Collection) value) == Type.STRING_ARRAY)) {
                    if (parameter.getAllowedValues() != null) {
                        checkAllowedValues(parameterId, value, parameter.getAllowedValues());
                    }
                    checkMinMaxSize(parameterId, (Collection) value, parameter.getMinArraySize(),
                            parameter.getMaxArraySize());
                    for (Object o : (Collection) value) {
                        if (parameter.getMinValue() != null && isNumeric(parameter.getMinValue().toString())) {
                            checkIsLessThan(parameterId, o.toString(), parameter.getMinValue().toString());
                        }
                        if (parameter.getMaxValue() != null && isNumeric(parameter.getMaxValue().toString())) {
                            checkIsGreaterThan(parameterId, o.toString(), parameter.getMaxValue().toString());
                        }
                    }
                } else {
                    throw new ParameterTypeException(
                            "Parameter with id: '" + parameterId + "' must be Array of alphanumeric values");
                }
                break;
            default:
                break;
            }
        }
    }
}

From source file:org.jaqpot.core.service.validator.ParameterValidator.java

private static Type getTypeOfCollection(Collection collection) {
    Type content = null;//w  w  w  .j a  v  a  2  s.c om
    for (Object value : collection) {
        if (!isNumeric(value.toString())) {
            if (!StringUtils.isAlphanumericSpace(value.toString())) {
                return Type.UNDEFINED;
            } else if (content == Type.NUMERIC_ARRAY) {
                return Type.UNDEFINED;
            } else {
                content = Type.STRING_ARRAY;
            }
        } else if (content == Type.STRING_ARRAY) {
            return Type.UNDEFINED;
        } else {
            content = Type.NUMERIC_ARRAY;
        }
    }
    return content;
}

From source file:org.jraf.irondad.handler.pixgame.PixGameHandler.java

private void newGame(Connection connection, String fromNickname, String searchTerms,
        HandlerContext handlerContext) throws IOException {
    boolean isRandom = RANDOM.equalsIgnoreCase(searchTerms);
    if (isRandom) {
        searchTerms = getRandomWord(handlerContext);
    } else if (!StringUtils.isAlphanumericSpace(searchTerms)) {
        connection.send(Command.PRIVMSG, fromNickname,
                "No punctuation allowed in the search terms.  Try another one.");
        return;//w w  w.ja v a2 s. c om
    }
    try {
        mSearchResultCount = queryGoogle(handlerContext, connection, searchTerms);
    } catch (IOException e) {
        Log.w(TAG, "newGame Could not query Google", e);
        connection.send(Command.PRIVMSG, fromNickname, "Oops something went wrong...");
        return;
    }
    if (mSearchResultCount == 0) {
        connection.send(Command.PRIVMSG, fromNickname, "This search has no results!  Try another one.");
        resetGame();
        return;
    }
    mSearchTerms = searchTerms;
    mGameCreatedBy = fromNickname;

    if (isRandom) {
        connection.send(Command.PRIVMSG, fromNickname, "New game started with a random word search.");
    } else {
        connection.send(Command.PRIVMSG, fromNickname,
                "New game started with the search \"" + searchTerms + "\".");
    }
}

From source file:rs.metropolitan.data_changer.DataFactory.java

public Object change(String data) {

    String trim = data.trim();// w ww . ja v a 2 s  . co  m
    System.out.println(trim);
    String[] seps = { ",", "{", "}" };
    if (StringUtils.containsAny(trim, seps)) {
        ToList lista = new ToList();
        return lista.changeString(trim);
    } else if (false) {

    } else if (NumberUtils.isDigits(trim)) {
        ToInteger toint = new ToInteger();
        return toint.changeString(trim);
    } else if (StringUtils.isAlphanumericSpace(trim)) {
        return data;
    }
    return null;

}

From source file:vista.controlador.Validador.java

/**
 * Para validar tanto cursos como temas. Esto es que slo contengan letras,
 * nmeros y espacios/*  w  w w.j  av a2 s .  co m*/
 *
 * @param campo la cadena de texto a validar
 * @return true si el campo es un curso/tema, false si no
 */
public static boolean esCurso(String campo) {
    boolean ok = true;

    if (estaVacio(campo) || !StringUtils.isAlphanumericSpace(campo)) {
        ok = false;
    }

    return ok;
}