Example usage for org.apache.commons.lang3 CharSetUtils keep

List of usage examples for org.apache.commons.lang3 CharSetUtils keep

Introduction

In this page you can find the example usage for org.apache.commons.lang3 CharSetUtils keep.

Prototype

public static String keep(final String str, final String... set) 

Source Link

Document

Takes an argument in set-syntax, see evaluateSet, and keeps any of characters present in the specified string.

 CharSetUtils.keep(null, *)        = null CharSetUtils.keep("", *)          = "" CharSetUtils.keep(*, null)        = "" CharSetUtils.keep(*, "")          = "" CharSetUtils.keep("hello", "hl")  = "hll" CharSetUtils.keep("hello", "le")  = "ell" 

Usage

From source file:org.apache.roller.weblogger.ui.struts2.admin.UserEdit.java

private void myValidate() {
    if (isAdd()) {
        String allowed = WebloggerConfig.getProperty("username.allowedChars");
        if (allowed == null || allowed.trim().length() == 0) {
            allowed = Register.DEFAULT_ALLOWED_CHARS;
        }/*from   w  w  w .ja v  a  2  s. c  o  m*/
        String safe = CharSetUtils.keep(getBean().getUserName(), allowed);

        if (StringUtils.isEmpty(getBean().getUserName())) {
            addError("error.add.user.missingUserName");
        } else if (!safe.equals(getBean().getUserName())) {
            addError("error.add.user.badUserName");
        }
        if ((authMethod == AuthMethod.ROLLERDB
                || (authMethod == AuthMethod.DB_OPENID && StringUtils.isEmpty(getBean().getOpenIdUrl())))
                && StringUtils.isEmpty(getBean().getPassword())) {
            addError("error.add.user.missingPassword");
        }
    } else {
        if (user.getUserName() == null) {
            addError("userAdmin.error.userNotFound");
        }
    }
    if ((authMethod == AuthMethod.OPENID) && StringUtils.isEmpty(getBean().getOpenIdUrl())) {
        addError("userRegister.error.missingOpenID");
    }

    // check that OpenID, if provided, is not taken
    if (!StringUtils.isEmpty(getBean().getOpenIdUrl())) {
        try {
            UserManager mgr = WebloggerFactory.getWeblogger().getUserManager();
            User user = mgr.getUserByOpenIdUrl(bean.getOpenIdUrl());
            if (user != null && !(user.getUserName().equals(bean.getUserName()))) {
                addError("error.add.user.openIdInUse");
            }
        } catch (WebloggerException ex) {
            log.error("error checking OpenID URL", ex);
            addError("generic.error.check.logs");
        }
    }
}

From source file:yoyo.framework.standard.shared.commons.lang.CharSetUtilsTest.java

@Test
public void test() {
    assertThat(CharSetUtils.count(null, "a-z"), is(0));
    assertThat(CharSetUtils.count("", "a-z"), is(0));
    assertThat(CharSetUtils.count("hello", "k-p"), is(3));
    assertThat(CharSetUtils.count("hello", "a-e"), is(1));
    assertThat(CharSetUtils.count("hello", "a", "b", "c", "d", "e"), is(1));
    assertThat(CharSetUtils.delete(null, "a-z"), is(nullValue()));
    assertThat(CharSetUtils.delete("", "a-z"), is(""));
    assertThat(CharSetUtils.delete("hello", "hl"), is("eo"));
    assertThat(CharSetUtils.keep(null, "a-z"), is(nullValue()));
    assertThat(CharSetUtils.keep("", "a-z"), is(""));
    assertThat(CharSetUtils.keep("hello", "hl"), is("hll"));
    assertThat(CharSetUtils.squeeze(null, "a-z"), is(nullValue()));
    assertThat(CharSetUtils.squeeze("", "a-z"), is(""));
    assertThat(CharSetUtils.squeeze("hello", "k-p"), is("helo"));
}