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

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

Introduction

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

Prototype

public static boolean containsOnly(final CharSequence cs, final String validChars) 

Source Link

Document

Checks if the CharSequence contains only certain characters.

A null CharSequence will return false .

Usage

From source file:SampleLang.java

/**
 * Check if the key is valid/*w ww. j  av  a  2s . com*/
 * @param key license key value
 * @return true if key is valid, false otherwise.
 */
public static boolean checkLicenseKey(String key) {
    //checks if empty or null
    if (StringUtils.isBlank(key)) {
        return false;
    }
    //delete all white space
    key = StringUtils.deleteWhitespace(key);
    //Split String using the - separator
    String[] keySplit = StringUtils.split(key, "-");
    //check lengths of whole and parts
    if (keySplit.length != 2 || keySplit[0].length() != 4 || keySplit[1].length() != 4) {
        return false;
    }
    //Check if first part is numeric
    if (!StringUtils.isNumeric(keySplit[0])) {
        return false;
    }
    //Check if second part contains only
    //the four characters 'J', 'A', 'V' and 'A'
    if (!StringUtils.containsOnly(keySplit[1], new char[] { 'J', 'A', 'V', 'A' })) {
        return false;
    }
    //Check if the fourth character
    //in the first part is a '0'
    if (StringUtils.indexOf(keySplit[0], '0') != 3) {
        return false;
    }
    //If all conditions are fulfilled, key is valid.
    return true;
}

From source file:kenh.expl.functions.ContainsOnly.java

public boolean process(String seq, String searchSeq) {
    return StringUtils.containsOnly(seq, searchSeq);
}

From source file:com.github.cherimojava.orchidae.util.FileUtil.java

/**
 * checks if the given id is 16 chars long and is based upon hex chars
 * /*from w w  w  . j  a v  a  2s. c  om*/
 * @param id
 * @return
 */
public static boolean validateId(String id) {
    return id != null && id.length() == 16 && StringUtils.containsOnly(id, hex);
}

From source file:alluxio.security.authorization.ModeParser.java

private Mode parseSymbolic(String value) {
    Mode.Bits ownerBits = Bits.NONE;//from   ww  w .j a  v a2 s .  c  o  m
    Mode.Bits groupBits = Bits.NONE;
    Mode.Bits otherBits = Bits.NONE;

    String[] specs = value.contains(",") ? value.split(",") : new String[] { value };
    for (String spec : specs) {
        String[] specParts = spec.split("=");

        // Validate that the spec is usable
        // Must have targets=perm i.e. 2 parts
        // Targets must be in u, g, o and a
        // Permissions must be in r, w and x
        if (specParts.length != 2) {
            throw new IllegalArgumentException(ExceptionMessage.INVALID_MODE_SEGMENT.getMessage(value, spec));
        }
        if (!StringUtils.containsOnly(specParts[0], VALID_TARGETS)) {
            throw new IllegalArgumentException(
                    ExceptionMessage.INVALID_MODE_TARGETS.getMessage(value, spec, specParts[0]));
        }
        if (!StringUtils.containsOnly(specParts[1], VALID_PERMISSIONS)) {
            throw new IllegalArgumentException(
                    ExceptionMessage.INVALID_MODE_PERMISSIONS.getMessage(value, spec, specParts[1]));
        }

        // Build the permissions being specified
        Mode.Bits specBits = Bits.NONE;
        for (char permChar : specParts[1].toCharArray()) {
            switch (permChar) {
            case 'r':
                specBits = specBits.or(Bits.READ);
                break;
            case 'w':
                specBits = specBits.or(Bits.WRITE);
                break;
            case 'x':
                specBits = specBits.or(Bits.EXECUTE);
                break;
            default:
                // Should never get here as already checked for invalid targets
            }
        }

        // Apply them to the targets
        for (char targetChar : specParts[0].toCharArray()) {
            switch (targetChar) {
            case 'u':
                ownerBits = ownerBits.or(specBits);
                break;
            case 'g':
                groupBits = groupBits.or(specBits);
                break;
            case 'o':
                otherBits = otherBits.or(specBits);
                break;
            case 'a':
                ownerBits = ownerBits.or(specBits);
                groupBits = groupBits.or(specBits);
                otherBits = otherBits.or(specBits);
                break;
            default:
                // Should never get here as already checked for invalid targets
            }
        }
    }

    // Return the resulting mode
    return new Mode(ownerBits, groupBits, otherBits);
}

From source file:com.nmote.smpp.DefaultSMPPAddressConverter.java

@Override
public SMPPAddress toSMPPAddress(String address) {
    int ton = SMPPAddress.TON_UNKNOWN;
    int npi = SMPPAddress.NPI_UNKNOWN;
    int len = address.length();
    if (len > 0) {
        boolean allPhoneDigits = StringUtils.containsOnly(address, PHONE_DIGITS);

        if (allPhoneDigits) {
            if (len <= ABBR_LEN) {
                ton = ABBR_TON;/*ww  w.  jav a2 s  .c  om*/
                npi = ABBR_NPI;
            } else if (address.startsWith(INT_PREFIX)) {
                ton = INT_TON;
                npi = INT_NPI;
                address = address.substring(INT_PREFIX.length());
            } else if (address.startsWith(INT_PREFIX_ZERO)) {
                ton = INT_TON;
                npi = INT_NPI;
                address = address.substring(INT_PREFIX_ZERO.length());
            } else if (address.startsWith(NAT_PREFIX_ZERO)) {
                ton = NAT_TON;
                npi = NAT_NPI;
                address = address.substring(NAT_PREFIX_ZERO.length());
            }
        } else {
            ton = ALPHA_TON;
            npi = ALPHA_NPI;
        }
    }
    return new SMPPAddress(address, ton, npi);
}

From source file:com.bellman.bible.service.format.osistohtml.taghandler.NoteHandler.java

@Override
public void end() {
    String noteText = writer.getTempStoreString();
    if (noteText.length() > 0) {
        if (!StringUtils.containsOnly(noteText, "[];()., ")) {
            Note note = new Note(verseInfo.currentVerseNo, currentNoteRef, noteText, NoteType.TYPE_GENERAL,
                    null, null);/*from  w  ww . j a  v a2 s  . co m*/
            notesList.add(note);
        }
        // and clear the buffer
        writer.clearTempStore();
    }
    isInNote = false;
    writer.finishWritingToTempStore();
}

From source file:de.jcup.egradle.eclipse.ide.console.EGradleConsoleStyleListener.java

@Override
public void lineGetStyle(LineStyleEvent event) {
    if (event == null) {
        return;//from   w w w .  j a va 2  s  . c  om
    }
    String lineText = event.lineText;
    if (StringUtils.isBlank(lineText)) {
        return;
    }
    /* styling */
    StyleRange defStyle;

    boolean atLeastOneStyle = event.styles != null && event.styles.length > 0;
    if (atLeastOneStyle) {
        defStyle = (StyleRange) event.styles[0].clone();
        if (defStyle.background == null) {
            defStyle.background = getColor(BLACK);
        }
    } else {
        defStyle = new StyleRange(1, lastRangeEnd, getColor(BLACK), getColor(WHITE), SWT.NORMAL);
    }

    lastRangeEnd = 0;

    List<StyleRange> ranges = new ArrayList<StyleRange>();
    boolean handled = false;
    /* line text */
    if (!handled) {
        if (StringUtils.containsOnly(lineText, "-")) {
            /* only a marker line from gradle */
            addRange(ranges, event.lineOffset, lineText.length(),
                    getColor(EGradleConsoleColorsConstants.BRIGHT_BLUE), true);
            handled = true;
        }
    }
    handled = handled || markLine(event, lineText, ranges, handled,
            SimpleProcessExecutor.MESSAGE__EXECUTION_CANCELED_BY_USER, BRIGHT_BLUE, true, BLUE, false);
    handled = handled
            || markLine(event, lineText, ranges, handled, "> Could not find", RED, false, BRIGHT_RED, false);
    handled = handled || markLine(event, lineText, ranges, handled,
            "Could not resolve all dependencies for configuration", RED, false, BRIGHT_RED, false);
    handled = handled
            || markLine(event, lineText, ranges, handled, "Could not resolve:", RED, false, BRIGHT_RED, false);
    handled = handled
            || markLine(event, lineText, ranges, handled, "Could not resolve", RED, false, RED, false);
    handled = handled
            || markLine(event, lineText, ranges, handled, "Downloading", BLUE, false, BRIGHT_BLUE, false);
    handled = handled
            || markLine(event, lineText, ranges, handled, "Download", BLUE, false, BRIGHT_BLUE, false);
    /* index parts and other */
    if (!handled) {
        for (ParseData data : SHARED_PARSE_DATA) {
            parse(event, defStyle, lineText, ranges, data);
        }
    }

    if (!ranges.isEmpty()) {
        event.styles = ranges.toArray(new StyleRange[ranges.size()]);
    }
}

From source file:com.thruzero.applications.faces.demo.beans.page.LoginBean.java

public void loginListener(ActionEvent event) throws AbortProcessingException {
    Subject currentSubject = userService.getCurrentSubject();

    if (!currentSubject.isAuthenticated()) {
        UsernamePasswordToken token = new UsernamePasswordToken(loginId, oneTimePw, nonce);

        try {//from   w  w w  .j  ava 2 s. c om
            // assert that password has been set to "***" (prevent introduction of bugs that might expose user password in production)
            if (StringUtils.isNotEmpty(password) && !StringUtils.containsOnly(password, "*")) {
                // this will be handled below
                throw new Exception("ERROR: Regression - password has not been set to '****'.");
            }

            // attempt to log in
            currentSubject.login(token);
            if (currentSubject.isAuthenticated()) {
                MessageUtils.addInfo("Success!");
                reset();
            } else {
                // this will be handled below
                throw new Exception("ERROR: User not authorized to access this resource.");
            }
        } catch (Exception e) {
            if (e instanceof MessageIdAuthenticationException) {
                ResourceProvider resourceProvider = ProviderLocator.locate(ResourceProvider.class);
                MessageUtils.addError(resourceProvider.getResource(e.getMessage()));
            } else {
                MessageUtils.addError(e);
            }
            updateNonce();
            password = "";
            throw new AbortProcessingException("FAILED to log in.");
        }
        token.clear(); // clear encrypted password
    }
}

From source file:br.com.fidias.chance4j.text.CharacterTest.java

@Test
public void chooseFromSymbols() throws ChanceException {
    options.setPoolType(TextOptions.PoolType.symbols);
    options.setCasing(TextOptions.Casing.both);
    char chr;//from w  w  w.  j a  va  2  s  .  com
    for (int i = 0; i < 1000; i++) {
        chr = chance.character(options);
        assertTrue("choose only from symbols",
                StringUtils.containsOnly(String.valueOf(chr), Character.SYMBOLS));
    }
}

From source file:net.sf.dynamicreports.test.jasper.tableofcontents.TableOfContents1Test.java

@Override
public void test() {
    super.test();

    numberOfPagesTest(3);//from w w w.j  av a2  s. c o m

    elementValueTest("title.textField1", "Table of contents");

    elementCountTest("detail.textField1", 6);
    int index = 0;
    for (int i = 0; i < 3; i++) {
        String anchorName = group1.getGroup().getName() + "_" + i * 24;

        elementValueTest("detail.textField1", index, "value" + (i + 1));
        JRPrintText text = (JRPrintText) getElementAt("detail.textField1", index);
        Assert.assertEquals("text anchor", anchorName, text.getHyperlinkAnchor());

        JRPrintText dots = (JRPrintText) getElementAt("detail.textField2", index);
        String value = JRStyledTextUtil.getInstance(DefaultJasperReportsContext.getInstance())
                .getTruncatedText(dots);
        Assert.assertTrue("dots", StringUtils.containsOnly(value, "."));
        Assert.assertEquals("dots anchor", anchorName, dots.getHyperlinkAnchor());

        JRPrintText pageIndex = (JRPrintText) getElementAt("detail.textField3", index);
        Assert.assertEquals("pageIndex anchor", anchorName, pageIndex.getHyperlinkAnchor());
        index++;

        anchorName = labelExpression.getName() + "_" + (i + 1) * 24;

        elementValueTest("detail.textField1", index, "group footer" + (i + 1));
        text = (JRPrintText) getElementAt("detail.textField1", index);
        Assert.assertEquals("text anchor", anchorName, text.getHyperlinkAnchor());

        dots = (JRPrintText) getElementAt("detail.textField2", index);
        value = JRStyledTextUtil.getInstance(DefaultJasperReportsContext.getInstance()).getTruncatedText(dots);
        Assert.assertTrue("dots", StringUtils.containsOnly(value, "."));
        Assert.assertEquals("dots anchor", anchorName, dots.getHyperlinkAnchor());

        pageIndex = (JRPrintText) getElementAt("detail.textField3", index);
        Assert.assertEquals("pageIndex anchor", anchorName, pageIndex.getHyperlinkAnchor());
        index++;
    }
    elementValueTest("detail.textField3", "1", "1", "1", "2", "2", "2");

    elementCountTest("detail.textField4", 9);
    for (int i = 0; i < 9; i++) {
        String anchorName = group2.getGroup().getName() + "_" + i * 8;

        JRPrintText text = (JRPrintText) getElementAt("detail.textField4", i);
        Assert.assertEquals("text anchor", anchorName, text.getHyperlinkAnchor());

        JRPrintText dots = (JRPrintText) getElementAt("detail.textField5", i);
        String value = JRStyledTextUtil.getInstance(DefaultJasperReportsContext.getInstance())
                .getTruncatedText(dots);
        Assert.assertTrue("dots", StringUtils.containsOnly(value, "."));
        Assert.assertEquals("dots anchor", anchorName, dots.getHyperlinkAnchor());

        JRPrintText pageIndex = (JRPrintText) getElementAt("detail.textField6", i);
        Assert.assertEquals("pageIndex anchor", anchorName, pageIndex.getHyperlinkAnchor());
    }
    elementValueTest("detail.textField4", "value1", "value2", "value3", "value1", "value2", "value3", "value1",
            "value2", "value3");
    elementValueTest("detail.textField6", "1", "1", "1", "1", "1", "1", "2", "2", "2");

    String name1 = "groupHeaderTitleAndValue.group_" + group1.getGroup().getName() + ".tocReference1";
    String name2 = "groupHeaderTitleAndValue.group_" + group1.getGroup().getName() + "1";
    elementCountTest(name1, 3);
    for (int i = 0; i < 3; i++) {
        String anchorName = group1.getGroup().getName() + "_" + i * 24;

        elementValueTest(name1, i, "");
        JRPrintText reference = (JRPrintText) getElementAt(name1, i);
        Assert.assertEquals("reference anchorName " + name1, anchorName, reference.getAnchorName());

        groupHeaderValueTest(group1, i, "value" + (i + 1));
        reference = (JRPrintText) getElementAt(name2, i);
        Assert.assertEquals("reference anchorName " + name2, anchorName, reference.getAnchorName());
    }

    name1 = labelExpression.getName() + ".tocReference";
    name2 = "groupFooter.textField1";
    elementCountTest(name1, 3);
    for (int i = 0; i < 3; i++) {
        String anchorName = labelExpression.getName() + "_" + (i + 1) * 24;

        elementValueTest(name1, i, "");
        JRPrintText reference = (JRPrintText) getElementAt(name1, i);
        Assert.assertEquals("reference anchorName " + name1, anchorName, reference.getAnchorName());

        groupHeaderValueTest(group1, i, "value" + (i + 1));
        reference = (JRPrintText) getElementAt(name2, i);
        Assert.assertEquals("reference anchorName " + name2, anchorName, reference.getAnchorName());
    }

    name1 = "groupHeaderTitleAndValue.group_" + group2.getGroup().getName() + ".tocReference1";
    name2 = "groupHeaderTitleAndValue.group_" + group2.getGroup().getName() + "1";
    elementCountTest(name1, 9);
    for (int i = 0; i < 9; i++) {
        String anchorName = group2.getGroup().getName() + "_" + i * 8;

        elementValueTest(name1, i, "");
        JRPrintText reference = (JRPrintText) getElementAt(name1, i);
        Assert.assertEquals("reference anchorName " + name1, anchorName, reference.getAnchorName());

        reference = (JRPrintText) getElementAt(name2, i);
        Assert.assertEquals("reference anchorName " + name2, anchorName, reference.getAnchorName());
    }
    groupHeaderValueTest(group2, "value1", "value2", "value3", "value1", "value2", "value3", "value1", "value2",
            "value3");

    columnTitleCountTest(column3, 2);
    columnTitleValueTest(column3, "Column3", "Column3");
    columnDetailCountTest(column3, 72);
    columnDetailValueTest(column3, 71, "text");
}