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

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

Introduction

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

Prototype

public static int countMatches(String str, String sub) 

Source Link

Document

Counts how many times the substring appears in the larger String.

Usage

From source file:com.kstenschke.shifter.models.shiftertypes.JsVariablesDeclarations.java

/**
 * @param  str      text selection to be shifted
 * @return String/*from  ww  w .ja  v  a  2  s .  co  m*/
 */
public static String getShifted(String str) {
    String[] lines = str.split("\n");
    String shiftedLines = "";

    int lineNumber = 0;
    String shiftedLine;
    for (String line : lines) {
        line = line.trim();

        if (line.isEmpty() || line.startsWith("//")) {
            // do not change empty or comment-lines
            shiftedLine = line;
        } else {
            // remove "var " from beginning
            line = line.substring(4);
            // replace ";" from ending by ",\n"
            if (StringUtils.countMatches(line, "//") == 1) {
                // handle line ending with comment intact
                String[] parts = line.split("//");
                parts[0] = parts[0].trim();
                shiftedLine = parts[0].substring(0, parts[0].length() - 1) + ", //" + parts[1];
            } else {
                shiftedLine = line.substring(0, line.length() - 1) + ",";
            }
        }

        shiftedLines += (lineNumber == 0 ? "" : "\t") + shiftedLine + "\n";
        lineNumber++;
    }

    return "var " + shiftedLines.substring(0, shiftedLines.length() - 2) + ";";
}

From source file:com.kstenschke.shifter.models.shiftertypes.CssUnit.java

/**
 * @param   stylesheet/*  www.  ja  v  a  2s .  c  om*/
 * @return   most prominently used unit of given stylesheet, 'px' if none used yet
 */
public static String determineMostProminentUnit(String stylesheet) {
    HashMap<String, Integer> map = new HashMap<String, Integer>();

    map.put(UNIT_CM, StringUtils.countMatches(stylesheet, UNIT_CM + ";"));
    map.put(UNIT_EM, StringUtils.countMatches(stylesheet, UNIT_EM + ";"));
    map.put(UNIT_IN, StringUtils.countMatches(stylesheet, UNIT_IN + ";"));
    map.put(UNIT_MM, StringUtils.countMatches(stylesheet, UNIT_MM + ";"));
    map.put(UNIT_PC, StringUtils.countMatches(stylesheet, UNIT_PC + ";"));
    map.put(UNIT_PT, StringUtils.countMatches(stylesheet, UNIT_PT + ";"));
    map.put(UNIT_PX, StringUtils.countMatches(stylesheet, UNIT_PX + ";"));

    int sum = UtilsMap.getSumOfValues(map);
    if (sum == 0) {
        return "px";
    }

    return UtilsMap.getKeyOfHighestValue(map);
}

From source file:loaders.LoadQueryTransformerTest.java

@Test
public void testReplaceSingleCondition() throws Exception {
    String query = "select id as id from user where id  =  ${param1}";
    HashMap<String, Object> params = new HashMap<String, Object>();
    AbstractDbDataLoader.QueryPack queryPack = prepareQuery(query, new BandData(""), params);
    System.out.println(queryPack.getQuery());
    Assert.assertFalse(queryPack.getQuery().contains("${"));
    writeParams(queryPack);/*  www.j a v  a2 s.  co m*/

    params.put("param1", "param1");
    queryPack = prepareQuery(query, new BandData(""), params);
    System.out.println(queryPack.getQuery());
    Assert.assertEquals(1, StringUtils.countMatches(queryPack.getQuery(), "?"));
    writeParams(queryPack);
}

From source file:de.tudarmstadt.ukp.dkpro.spelling.experiments.errormining.SentenceFilter.java

@Override
public void process(JCas aJCas) throws AnalysisEngineProcessException {

    Collection<Sentence> toRemove = new ArrayList<Sentence>();
    for (Sentence s : JCasUtil.select(aJCas, Sentence.class)) {
        String text = s.getCoveredText();

        // too many ":" -> likely to be Wikipedia language link list
        if (StringUtils.countMatches(text, ":") > 3) {
            toRemove.add(s);//from   ww  w. j a  va2  s  . c o  m
        }

        // remove very short sentences
        if (text.length() < 5) {
            toRemove.add(s);
        }
    }

    // TODO should we also remove underlying tokens?
    // as we add context according to sentences - it might not be necessary
    for (Sentence s : toRemove) {
        s.removeFromIndexes();
    }
}

From source file:com.agapsys.utils.console.printer.tables.Cell.java

public int getLineCount() {
    return StringUtils.countMatches(getWrappedValue(), "\n") + 1;
}

From source file:edu.utah.further.core.data.util.UTestSqlUtil.java

@Test
public void unlimitedInValues() {
    final List<Integer> list = CollectionUtil.newList();
    for (int i = 0; i < MAX_IN + 1; i++) {
        list.add(new Integer(i));
    }/*from w w w  . j  a v  a  2 s  . c o m*/
    final String inStatement = SqlUtil.unlimitedInValues(list, "DEAD_BEEF");
    assertThat(new Integer(StringUtils.countMatches(inStatement, "or")), is(new Integer(1)));
    assertThat(new Integer(StringUtils.countMatches(inStatement, "?")), is(new Integer(MAX_IN + 1)));

}

From source file:com.thesmartweb.swebrank.Sindice.java

/**
 * Method that gets the amount of semantic triples and recognizes the semantic namespaces used
 * @param url the url that we are going to get the stats for
 * @return the amount of semantic triples
 *///from  w  w  w  . j  a  v a  2  s  .c om
public int getsindicestats(String url) {
    try {
        String chk;
        String line = "";
        APIconn sind = new APIconn();
        chk = sind.check_conn(url);
        if (chk.equalsIgnoreCase("ok-conn")) {
            URL link_ur = new URL("http://api.sindice.com/v2/live?url=" + url + "&format=json");
            line = sind.connect(link_ur);
            line = "fail";
        }
        if (!line.equalsIgnoreCase("fail")) {
            JSONparsing gg = new JSONparsing();
            String chck = "\"o\":";
            triplecount = StringUtils.countMatches(line, chck);
            namespaces = gg.TripleParse(line);
            //triplecount=0;
        }
        return triplecount;
    } catch (IOException ex) {
        Logger.getLogger(Sindice.class.getName()).log(Level.SEVERE, null, ex);
        return triplecount;
    }
}

From source file:com.vaushell.treetasker.application.actionbar.TaskerCoachParserAction.java

@Override
public void tagStart(XMLPath path) {
    String pathValue = path.toString();
    if (pathValue.endsWith("/task")) {
        currentTask = new TT_Task();
        currentTask.setID(UUID.randomUUID().toString());
        currentTask.setLastModificationDate(new Date());
        currentTask.setStatus(TT_Task.TODO);
        int taskLvl = StringUtils.countMatches(pathValue, "/task");
        if (taskLvl == ROOT_LVL) {
            controller.addRootTask(currentTask);
        } else {/*from w w  w.  j a  v  a  2s .  com*/
            controller.addTask(currentTask, taskLvlMap.get(taskLvl - 1));
        }
        // Store the ID of the last node added at taskLvl
        taskLvlMap.put(taskLvl, currentTask.getID());
    }

}

From source file:com.dycody.android.idealnote.helpers.NotesHelperTest.java

@Test
public void mergeNotes() {

    int notesNumber = 3;
    List<Note> notes = new ArrayList<>();
    for (int i = 0; i < notesNumber; i++) {
        Note note = new Note();
        note.setTitle("Merged note " + i + " title");
        note.setContent("Merged note " + i + " content");
        notes.add(note);//w  w w  .  jav a  2 s  . c  om
    }
    Note mergeNote = NotesHelper.mergeNotes(notes, false);

    assertNotNull(mergeNote);
    assertTrue(mergeNote.getTitle().equals("Merged note 0 title"));
    assertTrue(mergeNote.getContent().contains("Merged note 0 content"));
    assertTrue(mergeNote.getContent().contains("Merged note 1 content"));
    assertTrue(mergeNote.getContent().contains("Merged note 2 content"));
    assertEquals(StringUtils.countMatches(mergeNote.getContent(), Constants.MERGED_NOTES_SEPARATOR), 2);
}

From source file:net.pixomania.crawler.W3C.parser.rules.editors.EditorsRule7.java

@Override
public ArrayList<Person> run(String url, Document doc) {
    ArrayList<Person> editorList = new ArrayList<>();

    Elements editors = doc.select("dt:contains(Authors/Editors) ~ dd, dt:contains(Author/Editor) ~ dd");
    if (editors.size() == 0)
        return null;

    boolean skip = false;
    for (Element editor : editors) {
        Element prev = editor.previousElementSibling();
        if (prev.tagName().equals("dt")) {
            if (!prev.text().trim().toLowerCase().startsWith("authors/editors")
                    && !prev.text().trim().toLowerCase().startsWith("author/editor")) {
                skip = true;//from   www.  java2  s.  c  o  m
            }
        }

        if (skip) {
            Element next = editor.nextElementSibling();
            if (next != null) {
                if (next.text().trim().toLowerCase().startsWith("authors/editors")
                        || next.text().trim().toLowerCase().startsWith("author/editor")) {
                    skip = false;
                    continue;
                }
            }
            continue;
        }

        if (StringUtils.countMatches(editor.text(), " - ") > 2) {
            Log.log("warning", url + ": This editor may be a list of editors separated by  - ");
            EditorsRule5 ed5 = new EditorsRule5();

            return ed5.run(url, doc);
        }

        String[] splitted = editor.html().split("<br />|<br clear=\"none\" />");

        if (splitted.length < 2) {
            if (editor.text().equals("WHATWG:") || editor.text().equals("W3C:"))
                continue;
            Person result = NameParser.parse(editor.text());
            if (result == null)
                continue;

            for (int i = 0; i < editor.select("a").size(); i++) {
                if (!editor.select("a").get(i).attr("href").isEmpty()) {
                    if (editor.select("a").get(i).attr("href").contains("@")) {
                        result.setEmail(editor.select("a").get(i).attr("href").replace("mailto:", ""));
                    } else {
                        result.addWebsite(editor.select("a").get(i).attr("href"));
                    }
                }
            }

            editorList.add(result);
        } else {
            for (String split : splitted) {
                if (!split.isEmpty()) {
                    if (split.equals("WHATWG:") || split.equals("W3C:"))
                        continue;
                    Document newdoc = Jsoup.parse(split.replaceAll("\n", ""));
                    Person result = NameParser.parse(newdoc.text());
                    if (result == null)
                        continue;

                    for (int i = 0; i < newdoc.select("a").size(); i++) {
                        if (!newdoc.select("a").get(i).attr("href").isEmpty()) {
                            if (newdoc.select("a").get(i).attr("href").contains("@")) {
                                result.setEmail(newdoc.select("a").get(i).attr("href").replace("mailto:", ""));
                            } else {
                                result.addWebsite(newdoc.select("a").get(i).attr("href"));
                            }
                        }
                    }

                    editorList.add(result);
                }
            }
        }

        Element next = editor.nextElementSibling();
        if (next != null)
            if (next.tag().getName().equals("dt"))
                break;
    }

    if (editorList.size() == 0)
        return null;

    return editorList;
}