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

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

Introduction

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

Prototype

public static int countMatches(final CharSequence str, final char ch) 

Source Link

Document

Counts how many times the char appears in the given string.

A null or empty ("") String input returns 0 .

 StringUtils.countMatches(null, *)       = 0 StringUtils.countMatches("", *)         = 0 StringUtils.countMatches("abba", 0)  = 0 StringUtils.countMatches("abba", 'a')   = 2 StringUtils.countMatches("abba", 'b')  = 2 StringUtils.countMatches("abba", 'x') = 0 

Usage

From source file:org.kie.workbench.common.forms.serialization.impl.FormDefinitionSerializerImplTest.java

protected String doSerializationTest() {
    String serializedForm = definitionSerializer.serialize(formDefinition);

    assertEquals(formDefinition.getFields().size(), StringUtils.countMatches(serializedForm, "\"code\""));
    assertNotNull(serializedForm);//  ww  w  .j  av a 2s . c  o m
    assertNotEquals(0, serializedForm.length());

    return serializedForm;
}

From source file:org.kitodo.data.elasticsearch.search.SearchRestClientIT.java

@Test
public void shouldGetDocumentByQuery() {
    await().untilAsserted(() -> assertEquals("Get of document has failed!",
            StringUtils.countMatches(searchRestClient.getDocument(query, null, null, null), "\"_id\""), 4));
}

From source file:org.kitodo.data.elasticsearch.search.SearchRestClientIT.java

@Test
public void shouldGetDocumentByQueryWithSize() {
    await().untilAsserted(() -> assertEquals("Get of document has failed!",
            StringUtils.countMatches(searchRestClient.getDocument(query, null, null, 3), "\"_id\""), 3));
}

From source file:org.kitodo.data.elasticsearch.search.SearchRestClientIT.java

@Test
public void shouldGetDocumentByQueryWithOffsetAndSize() {
    await().untilAsserted(() -> assertEquals("Get of document has failed!",
            StringUtils.countMatches(searchRestClient.getDocument(query, null, 2, 3), "\"_id\""), 2));
}

From source file:org.kuali.coeus.common.framework.person.signature.PersonSignatureLocationHelper.java

/**
 * This method is to find the count of first character in the signature tag
 * @return/*www .  ja v  a2s .  c  om*/
 */
protected int getCountMatchOfSignatureTagFirstCharacter() {
    String firstChar = Character.toString(getCurrentSignatureTag().charAt(0));
    return StringUtils.countMatches(getCurrentSignatureTag(), firstChar);
}

From source file:org.languagetool.commandline.MainTest.java

@Test
public void testPolishApiStdInDefaultOff() throws Exception {
    String test = "To jest test, ktry zrobiem, ktry mi si podoba.";
    System.setIn(new ByteArrayInputStream(test.getBytes()));
    String[] args = { "--api", "-l", "pl", "-eo", "-e", "PL_WORD_REPEAT", "-" };
    Main.main(args);//from w  ww.jav  a2s  .c  o  m
    String output = new String(this.out.toByteArray());
    assertThat(StringUtils.countMatches(output, "<error "), is(1));
    assertThat(StringUtils.countMatches(output, "<matches "), is(1));
    assertThat(StringUtils.countMatches(output, "</matches>"), is(1)); // https://github.com/languagetool-org/languagetool/issues/251
}

From source file:org.languagetool.commandline.MainTest.java

@Test
public void testPolishApiStdInDefaultOffNoErrors() throws Exception {
    String test = "To jest test.";
    System.setIn(new ByteArrayInputStream(test.getBytes()));
    String[] args = { "--api", "-l", "pl", "-e", "PL_WORD_REPEAT", "-" };
    Main.main(args);//from   w  ww.jav a  2  s  .  c om
    String output = new String(this.out.toByteArray());
    assertThat(StringUtils.countMatches(output, "<error "), is(0));
    assertThat(StringUtils.countMatches(output, "<matches "), is(1));
    assertThat(StringUtils.countMatches(output, "</matches>"), is(1));
}

From source file:org.languagetool.dev.RuleOverview.java

private int countXmlRules(String xmlRules) {
    return StringUtils.countMatches(xmlRules, "<rule "); // rules with IDs
}

From source file:org.languagetool.dev.RuleOverview.java

private int countXmlRuleGroupRules(String xmlRules) {
    return StringUtils.countMatches(xmlRules, "<rule>"); // rules in rule groups have no ID
}

From source file:org.languagetool.dev.wikipedia.SuggestionReplacer.java

/**
 * Applies the suggestions from the rule to the original text. For rules that
 * have no suggestion, a pseudo-correction is generated that contains the same
 * text as before./*from ww w  .  j  a  va  2 s.  c om*/
 */
public List<RuleMatchApplication> applySuggestionsToOriginalText(RuleMatch match) {
    List<String> replacements = new ArrayList<>(match.getSuggestedReplacements());
    boolean hasRealReplacements = replacements.size() > 0;
    if (!hasRealReplacements) {
        // create a pseudo replacement with the error text itself
        String plainText = textMapping.getPlainText();
        replacements.add(plainText.substring(match.getFromPos(), match.getToPos()));
    }

    List<RuleMatchApplication> ruleMatchApplications = new ArrayList<>();
    Location fromPosLocation = textMapping.getOriginalTextPositionFor(match.getFromPos() + 1); // not zero-based!
    Location toPosLocation = textMapping.getOriginalTextPositionFor(match.getToPos() + 1);

    /*System.out.println("=========");
    System.out.println(textMapping.getMapping());
    System.out.println("=========");
    System.out.println(textMapping.getPlainText());
    System.out.println("=========");
    System.out.println(originalText);
    System.out.println("=========");*/

    int fromPos = LocationHelper.absolutePositionFor(fromPosLocation, originalText);
    int toPos = LocationHelper.absolutePositionFor(toPosLocation, originalText);
    for (String replacement : replacements) {
        String errorText = textMapping.getPlainText().substring(match.getFromPos(), match.getToPos());
        // the algorithm is off a bit sometimes due to the complex syntax, so consider the next whitespace:
        int contextFrom = findNextWhitespaceToTheLeft(originalText, fromPos);
        int contextTo = findNextWhitespaceToTheRight(originalText, toPos);

        /*System.out.println(match + ":");
        System.out.println("match.getFrom/ToPos(): " + match.getFromPos() + "/" + match.getToPos());
        System.out.println("from/toPosLocation: " + fromPosLocation + "/" + toPosLocation);
        System.out.println("from/toPos: " + fromPos + "/" + toPos);
        System.out.println("contextFrom/To: " + contextFrom + "/" + contextTo);*/

        String context = originalText.substring(contextFrom, contextTo);
        String text = originalText.substring(0, contextFrom) + errorMarker.getStartMarker() + context
                + errorMarker.getEndMarker() + originalText.substring(contextTo);
        String newContext;
        if (StringUtils.countMatches(context, errorText) == 1) {
            newContext = context.replace(errorText, replacement);
        } else {
            // This may happen especially for very short strings. As this is an
            // error, we don't claim to have real replacements:
            newContext = context;
            hasRealReplacements = false;
        }
        String newText = originalText.substring(0, contextFrom)
                // we do a simple string replacement as that works even if our mapping is off a bit:
                + errorMarker.getStartMarker() + newContext + errorMarker.getEndMarker()
                + originalText.substring(contextTo);
        RuleMatchApplication application;
        if (hasRealReplacements) {
            application = RuleMatchApplication.forMatchWithReplacement(match, text, newText, errorMarker);
        } else {
            application = RuleMatchApplication.forMatchWithoutReplacement(match, text, newText, errorMarker);
        }
        ruleMatchApplications.add(application);
    }
    return ruleMatchApplications;
}