Example usage for org.apache.commons.lang StringUtils containsOnly

List of usage examples for org.apache.commons.lang StringUtils containsOnly

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils containsOnly.

Prototype

public static boolean containsOnly(String str, String validChars) 

Source Link

Document

Checks if the String contains only certain characters.

Usage

From source file:org.pentaho.di.core.util.KeyValue.java

/**
 * @param lowerKey/* ww w . j  a  v a2 s .co m*/
 *          key to test.
 * @throws IllegalArgumentException
 *           if key is invalid.
 */
public static final void assertKey(final String lowerKey) throws IllegalArgumentException {
    Assert.assertNotEmpty(lowerKey, "Key cannot be null or empty");
    if (!StringUtils.containsOnly(lowerKey, VALID_KEY_CHARS)) {
        throw new IllegalArgumentException(
                "Key contains invalid characters [validKeyCharacters=" + VALID_KEY_CHARS + "]");
    }
    if (lowerKey.charAt(0) == '-') {
        throw new IllegalArgumentException("Key must not start with '-'");
    }
    if (lowerKey.endsWith("-")) {
        throw new IllegalArgumentException("Key must not end with '-'");
    }
    if ("_".equals(lowerKey)) {
        throw new IllegalArgumentException("Key must not be  '_'");
    }
}

From source file:org.projectforge.common.StringHelper.java

/** Valid characters are ''+'' as first char, ''-'', ''/'' and spaces. The leading country code is mandatory, e. g.: +49 561 316793-0 */
public static boolean checkPhoneNumberFormat(final String value) {
    if (StringUtils.isBlank(value) == true) {
        return true;
    }/*from w  w w . j  ava2s . co m*/
    if (StringUtils.containsOnly(value, "+1234567890 -/") == false || value.startsWith("+") == false
            || value.length() < 2 || Character.isDigit(value.charAt(1)) == false
            || value.indexOf('+', 1) != -1) {
        return false;
    }
    final String str = removeWhitespaces(value);
    if (str.startsWith("+49") && str.charAt(3) == '0') {
        // +49 0561 123456 is not allowed
        return false;
    }
    return true;
}

From source file:org.projectforge.web.address.PhoneCallPage.java

void call() {
    final boolean extracted = processPhoneNumber();
    if (extracted == true) {
        return;//from   www .  j a  va2s  .c  o  m
    }
    if (StringUtils.containsOnly(form.getPhoneNumber(), "0123456789+-/() ") == false) {
        form.addError("address.phoneCall.number.invalid");
        return;
    }
    form.setPhoneNumber(extractPhonenumber(form.getPhoneNumber()));
    callNow();
}

From source file:org.projectforge.web.address.PhoneLookUpServlet.java

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
        throws ServletException, IOException {
    final String number = req.getParameter("nr");
    if (StringUtils.isBlank(number) == true || StringUtils.containsOnly(number, "+1234567890 -/") == false) {
        log.warn(/*from w w  w  . ja  v a  2s.  co  m*/
                "Bad request, request parameter nr not given or contains invalid characters (only +0123456789 -/ are allowed): "
                        + number);
        resp.sendError(HttpStatus.SC_BAD_REQUEST);
        return;
    }

    final String key = req.getParameter("key");
    final String expectedKey = ConfigXml.getInstance().getPhoneLookupKey();
    if (StringUtils.isBlank(expectedKey) == true) {
        log.warn(
                "Servlet call for receiving phonelookups ignored because phoneLookupKey is not given in config.xml file.");
        resp.sendError(HttpStatus.SC_BAD_REQUEST);
        return;
    }
    if (expectedKey.equals(key) == false) {
        log.warn("Servlet call for phonelookups ignored because phoneLookupKey does not match given key: "
                + key);
        resp.sendError(HttpStatus.SC_FORBIDDEN);
        return;
    }

    final String searchNumber = NumberHelper.extractPhonenumber(number);
    final AddressDao addressDao = (AddressDao) Registry.instance().getDao(AddressDao.class);

    final BaseSearchFilter filter = new BaseSearchFilter();
    filter.setSearchString("*" + searchNumber);
    final QueryFilter queryFilter = new QueryFilter(filter);

    final StringBuffer buf = new StringBuffer();
    // Use internal get list method for avoiding access checking (no user is logged-in):
    final List<AddressDO> list = addressDao.internalGetList(queryFilter);
    if (list != null && list.size() >= 1) {
        AddressDO result = list.get(0);
        if (list.size() > 1) {
            // More than one result, therefore find the newest one:
            buf.append("+"); // Mark that more than one entry does exist.
            for (final AddressDO matchingUser : list) {
                if (matchingUser.getLastUpdate().after(result.getLastUpdate()) == true) {
                    result = matchingUser;
                }
            }
        }
        resp.setContentType("text/plain");
        final String fullname = result.getFullName();
        final String organization = result.getOrganization();
        StringHelper.listToString(buf, "; ", fullname, organization);
        resp.getOutputStream().print(buf.toString());
    } else {
        /* mit Thomas abgesprochen. */
        resp.getOutputStream().print(0);
    }
}

From source file:org.sonar.plugins.scm.git.GitBlameConsumer.java

private void consumeRevisionLine(String line) {
    String[] parts = line.split("\\s", 4);

    if (parts.length >= 1) {
        revision = parts[0];// ww w  .j av  a2  s .  c o  m

        if (StringUtils.containsOnly(revision, "0")) {
            throw new IllegalStateException("Unable to blame file " + filename + ". No blame info at line "
                    + (getLines().size() + 1) + ". Is file commited?");
        }

        BlameLine oldLine = commitInfo.get(revision);

        if (oldLine != null) {
            // restore the commit info
            author = oldLine.author();
            committer = oldLine.committer();
            time = oldLine.date();
        }

        expectRevisionLine = false;
    }
}

From source file:org.talend.dataprep.util.NumericHelper.java

/**
 * Checks whether <code>str</code> can be parsed by {@link BigDecimalParser} without throwing an exception.
 * @param str The string to be tested, can be <code>null</code> or empty.
 * @return <code>true</code> if string can be parsed by {@link BigDecimalParser}, <code>false</code> otherwise.
 *///from   ww  w.  j  ava2 s  .  com
public static boolean isBigDecimal(String str) {
    if (StringUtils.isEmpty(str)) {
        return false;
    }
    // Check for (nnnn) values (negative values in accounting).
    String strForValidation = StringUtils.remove(str, ' ');
    if (strForValidation.lastIndexOf('(') == 0
            && strForValidation.lastIndexOf(')') == strForValidation.length() - 1) {
        strForValidation = strForValidation.substring(1, strForValidation.length() - 1); // Keep only nnnn
    }

    if (!StringUtils.containsOnly(strForValidation, ALLOWED_NUMERIC_CHARACTERS)
            && !isValid(strForValidation, new BigDecimalValidator())) {
        return false;
    }

    // Support for values that starts with ',' or '.' (like .5 or ,5).
    if (strForValidation.charAt(0) == ',' || strForValidation.charAt(0) == '.') {
        return true;
    }

    // Try custom decimal formats
    DecimalFormat[] supportedFormats = { BigDecimalParser.EU_DECIMAL_PATTERN,
            BigDecimalParser.EU_SCIENTIFIC_DECIMAL_PATTERN, BigDecimalParser.US_DECIMAL_PATTERN,
            BigDecimalParser.US_SCIENTIFIC_DECIMAL_PATTERN };
    for (DecimalFormat supportedFormat : supportedFormats) {
        try {
            if (supportedFormat.parse(strForValidation) != null) {
                return true;
            }
        } catch (ParseException e) {
            LOGGER.debug("Unable to parse '{}' using custom decimal format '{}'.", strForValidation,
                    supportedFormat.toPattern(), e);
        }
    }

    return false;
}