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.github.rnewson.couchdb.lucene.LuceneServlet.java

private void doPostInternal(final HttpServletRequest req, final HttpServletResponse resp)
        throws IOException, JSONException {
    switch (StringUtils.countMatches(req.getRequestURI(), "/")) {
    case 3:/*w w  w  .  ja  v  a2 s  .co m*/
        if (req.getPathInfo().endsWith("/_cleanup")) {
            cleanup(req, resp);
            return;
        }
        break;
    case 5: {
        final DatabaseIndexer indexer = getIndexer(req);
        if (indexer == null) {
            ServletUtils.sendJsonError(req, resp, 500, "error_creating_index");
            return;
        }
        indexer.search(req, resp);
        break;
    }
    case 6:
        final DatabaseIndexer indexer = getIndexer(req);
        indexer.admin(req, resp);
        return;
    }
    ServletUtils.sendJsonError(req, resp, 400, "bad_request");
}

From source file:de.tudarmstadt.ukp.clarin.webanno.tsv.WebannoTsv3Reader.java

/**
 * Iterate through lines and create span annotations accordingly. For
 * multiple span annotation, based on the position of the annotation in the
 * line, update only the end position of the annotation
 *//*from   ww  w.j  a va 2s.c om*/
private void setAnnotations(JCas aJCas, InputStream aIs, String aEncoding) throws IOException {

    // getting header information
    LineIterator lineIterator = IOUtils.lineIterator(aIs, aEncoding);
    int sentBegin = -1, sentEnd = 0;
    int prevSentEnd = 0;
    StringBuilder sentLineSb = new StringBuilder();
    String lastSent = "";
    while (lineIterator.hasNext()) {
        String line = lineIterator.next();
        if (line.startsWith("#T_")) {
            setLayerAndFeature(aJCas, line);
            continue;
        }

        if (line.startsWith("#Text=")) {
            if (sentLineSb.toString().isEmpty()) {
                sentLineSb.append(line.substring(line.indexOf("=") + 1));
            } else {
                sentLineSb.append(LF + line.substring(line.indexOf("=") + 1));
            }
            lastSent = sentLineSb.toString();
            continue;
        }
        if (line.startsWith("#FORMAT=")) {
            continue;
        }
        if (line.trim().isEmpty()) {
            if (!sentLineSb.toString().isEmpty()) {
                createSentence(aJCas, sentLineSb.toString(), sentBegin, sentEnd, prevSentEnd);
                prevSentEnd = sentEnd;
                sentBegin = -1;// reset for next sentence begin
                sentLineSb = new StringBuilder();
            }

            continue;
        }

        line = line.trim();
        int count = StringUtils.countMatches(line, "\t");

        if (columns != count) {
            throw new IOException(fileName + " This is not a valid TSV File. check this line: " + line);
        }

        String regex = "(?<!\\\\)*" + Pattern.quote(TAB);
        String[] lines = line.split(regex);

        int begin = Integer.parseInt(lines[1].split("-")[0]);
        int end = Integer.parseInt(lines[1].split("-")[1]);
        if (sentBegin == -1) {
            sentBegin = begin;
        }
        sentEnd = end;

        AnnotationUnit unit = createTokens(aJCas, lines, begin, end);

        int ind = 3;

        setAnnosPerTypePerUnit(lines, unit, ind);
    }

    // the last sentence
    if (!lastSent.isEmpty()) {
        createSentence(aJCas, lastSent, sentBegin, sentEnd, prevSentEnd);
    }

    Map<Type, Map<AnnotationUnit, List<AnnotationFS>>> annosPerTypePerUnit = new HashMap<>();
    setAnnosPerUnit(aJCas, annosPerTypePerUnit);
    addAnnotations(aJCas, annosPerTypePerUnit);
    addChainAnnotations(aJCas);
}

From source file:com.ibm.bi.dml.runtime.io.ReaderTextCSVParallel.java

/**
 * // w w  w.  j  a v  a 2s . c  o  m
 * @param path
 * @param job
 * @param hasHeader
 * @param delim
 * @return
 * @throws IOException
 * @throws DMLRuntimeException 
 */
private MatrixBlock computeCSVSizeAndCreateOutputMatrixBlock(InputSplit[] splits, Path path, JobConf job,
        boolean hasHeader, String delim, long estnnz) throws IOException, DMLRuntimeException {
    int nrow = 0;
    int ncol = 0;

    FileInputFormat.addInputPath(job, path);
    TextInputFormat informat = new TextInputFormat();
    informat.configure(job);

    // count no of entities in the first non-header row
    LongWritable key = new LongWritable();
    Text oneLine = new Text();
    RecordReader<LongWritable, Text> reader = informat.getRecordReader(splits[0], job, Reporter.NULL);
    try {
        if (reader.next(key, oneLine)) {
            String cellStr = oneLine.toString().trim();
            ncol = StringUtils.countMatches(cellStr, delim) + 1;
        }
    } finally {
        IOUtilFunctions.closeSilently(reader);
    }

    // count rows in parallel per split
    try {
        ExecutorService pool = Executors.newFixedThreadPool(_numThreads);
        ArrayList<CountRowsTask> tasks = new ArrayList<CountRowsTask>();
        for (InputSplit split : splits) {
            tasks.add(new CountRowsTask(split, informat, job, hasHeader));
            hasHeader = false;
        }
        pool.invokeAll(tasks);
        pool.shutdown();

        // collect row counts for offset computation
        // early error notify in case not all tasks successful
        _offsets = new SplitOffsetInfos(tasks.size());
        for (CountRowsTask rt : tasks) {
            if (!rt.getReturnCode())
                throw new IOException("Count task for csv input failed: " + rt.getErrMsg());
            _offsets.setOffsetPerSplit(tasks.indexOf(rt), nrow);
            _offsets.setLenghtPerSplit(tasks.indexOf(rt), rt.getRowCount());
            nrow = nrow + rt.getRowCount();
        }
    } catch (Exception e) {
        throw new IOException("Threadpool Error " + e.getMessage(), e);
    }

    // allocate target matrix block based on given size; 
    // need to allocate sparse as well since lock-free insert into target
    return createOutputMatrixBlock(nrow, ncol, estnnz, true, true);
}

From source file:com.ibm.bi.dml.runtime.io.ReaderTextCSV.java

/**
 * //from  w  w w .ja  va 2  s .  c  o  m
 * @param files
 * @param job
 * @param fs
 * @param hasHeader
 * @param delim
 * @param fill
 * @param fillValue
 * @return
 * @throws IOException
 */
private MatrixBlock computeCSVSize(List<Path> files, JobConf job, FileSystem fs, boolean hasHeader,
        String delim, boolean fill, double fillValue) throws IOException {
    int nrow = -1;
    int ncol = -1;
    String value = null;

    String cellStr = null;
    for (int fileNo = 0; fileNo < files.size(); fileNo++) {
        BufferedReader br = new BufferedReader(new InputStreamReader(fs.open(files.get(fileNo))));
        try {
            // Read the header line, if there is one.
            if (fileNo == 0) {
                if (hasHeader)
                    br.readLine(); //ignore header
                if ((value = br.readLine()) != null) {
                    cellStr = value.toString().trim();
                    ncol = StringUtils.countMatches(cellStr, delim) + 1;
                    nrow = 1;
                }
            }

            while ((value = br.readLine()) != null) {
                nrow++;
            }
        } finally {
            IOUtilFunctions.closeSilently(br);
        }
    }

    //create new matrix block (assume sparse for consistency w/ compiler)
    return new MatrixBlock(nrow, ncol, true);
}

From source file:com.redsqirl.workflow.utils.jdbc.GenericConfFile.java

@Override
protected String getRsTypeFileContent() {
    String ans = "";

    List<FieldType> priority = new ArrayList<FieldType>(9);
    priority.add(FieldType.BOOLEAN);/*from w  w w .  j av a  2s  .c  o  m*/
    priority.add(FieldType.INT);
    priority.add(FieldType.LONG);
    priority.add(FieldType.FLOAT);
    priority.add(FieldType.DOUBLE);
    priority.add(FieldType.DATE);
    priority.add(FieldType.DATETIME);
    priority.add(FieldType.TIMESTAMP);
    priority.add(FieldType.CHAR);
    priority.add(FieldType.STRING);
    try {
        ResultSet rs = databaseMetaData.getTypeInfo();
        Map<String, FieldType> mapAns = new LinkedHashMap<String, FieldType>();
        while (rs.next()) {
            String typeName = rs.getString(1).toUpperCase();
            int dataType = rs.getInt(2);
            //String precision = rs.getString(3).toUpperCase();
            //String lit_pref = rs.getString(4).toUpperCase();
            //String lit_suf = rs.getString(5).toUpperCase();
            String create_params = rs.getString(6);
            //String nullable = rs.getString(7).toUpperCase();
            //String case_sensitive = rs.getString(8).toUpperCase();
            //String seachable = rs.getString(9).toUpperCase();
            //String unsigned_attr = rs.getString(10).toUpperCase();
            if (typeRecognized.containsKey(dataType)) {
                //Type Recognized: Add it in the list
                int numberParam = 0;
                if (create_params != null && !create_params.isEmpty()) {
                    logger.info("params: " + create_params);
                    numberParam = StringUtils.countMatches(create_params, ",") + 1;
                } else {
                    logger.info("No parameters for " + dataType);
                }
                if (numberParam > 0) {
                    String reg = typeName;
                    reg += "\\(";
                    for (int i = 0; i < numberParam; ++i) {
                        if (i > 0) {
                            reg += ",";
                        }
                        reg += "\\d+";
                        String regCur = reg + "\\)";
                        if (!mapAns.containsKey(regCur) || priority
                                .indexOf(typeRecognized.get(dataType)) > priority.indexOf(mapAns.get(regCur))) {
                            mapAns.put(regCur, typeRecognized.get(dataType));
                        }
                    }
                } else {
                    mapAns.put(typeName, typeRecognized.get(dataType));
                }
            }
        }
        rs.close();
        Iterator<String> it = mapAns.keySet().iterator();
        while (it.hasNext()) {
            String cur = it.next();
            ans += cur;
            ans += ":" + mapAns.get(cur);
            ans += "\n";
            if (!cur.endsWith(")") && FieldType.STRING.equals(mapAns.get(cur)) && cur.startsWith("VAR")) {
                ans += cur;
                ans += "\\(\\d+\\)";
                ans += ":" + mapAns.get(cur);
                ans += "\n";
            }
        }
    } catch (Exception e) {
        logger.error(e, e);
        ans = new HiveConfFile().getRsTypeFileContent();
    }

    return ans;
}

From source file:edu.cmu.lti.f12.hw2.hw2_team01.passage.SentenceBasedPassageCandidateFinder.java

private List<PassageSpan> splitParagraph(String text) {

    StringBuffer temptext = new StringBuffer(text);
    List<PassageSpan> rawSpans = new ArrayList<PassageSpan>();
    List<PassageSpan> splitedSpans = new ArrayList<PassageSpan>();
    List<PassageSpan> sentences = new ArrayList<PassageSpan>();
    int start = 0;
    int startindice = -1;
    int pstart, pend, textend;
    while ((startindice = temptext.toString().toLowerCase().indexOf("<p>")) != -1) {

        temptext.delete(0, startindice + 3);
        start += (startindice + 3);/*  w w  w  .  j  a v  a2  s. c  o  m*/

        pstart = temptext.toString().toLowerCase().indexOf("<p>");
        pend = temptext.toString().toLowerCase().indexOf("</p>");
        textend = temptext.toString().toLowerCase().indexOf("</txt>");

        if (textend != -1 && textend * pend < pend * pend && textend * pstart < pstart * pstart) {
            PassageSpan ps = new PassageSpan(start, start + textend);

            rawSpans.add(ps);
            temptext = temptext.delete(0, textend + 6);
            start += (textend + 6);

        }

        else if (pend != -1 && pstart > pend) {
            PassageSpan ps = new PassageSpan(start, start + pend);
            rawSpans.add(ps);
            temptext = temptext.delete(0, pend + 4);
            start += (pend + 4);

        }

        else if (pstart != -1) {
            PassageSpan ps = new PassageSpan(start, start + pstart);
            rawSpans.add(ps);
            temptext = temptext.delete(0, pstart + 3);
            start += pstart + 3;

        } else {
        }
    }

    String substring = new String();
    String subtoken = new String();
    String cleanText = new String();
    PassageSpan rawps;
    int substart, subend, psstart, psend, offset;
    Iterator<PassageSpan> ips = rawSpans.iterator();
    while (ips.hasNext()) {
        sentences.clear();
        rawps = ips.next();
        //REFINE THE PARAGRAPH
        //rawps = RefinePassage(rawps);
        //if(rawps == null)continue;
        substart = rawps.begin;
        subend = rawps.end;
        substring = text.substring(substart, subend);
        int count = StringUtils.countMatches(substring, " ");
        if (substring.length() > 20 && count / (double) substring.length() <= 0.4) {
            cleanText = Jsoup.parse(substring).text().replaceAll("([\177-\377\0-\32]*)", "");
            if ((cleanText.length() / (double) substring.length()) >= 0.6) {
                StringTokenizer tokenizer = new StringTokenizer(substring, ",.?!;");
                psstart = substart;
                psend = psstart;
                offset = 0;
                while (tokenizer.hasMoreTokens()) {
                    psstart = psend;
                    subtoken = tokenizer.nextToken();
                    // offset = noneblankindex(subtoken);
                    // psstart += offset;

                    psend += subtoken.length() + 1;
                    int totallength = subtoken.length();
                    while (totallength <= 50) {
                        if (tokenizer.hasMoreElements()) {
                            subtoken = tokenizer.nextToken();
                            totallength += subtoken.length();
                            psend += subtoken.length() + 1;
                        } else
                            break;
                    }
                    PassageSpan sentence = new PassageSpan(psstart, psend);
                    sentences.add(sentence);
                }
            }
        }

        Iterator<PassageSpan> isentence = sentences.iterator();
        PassageSpan ps1;
        while (isentence.hasNext()) {
            ps1 = isentence.next();
            splitedSpans.add(ps1);

        }

        MergeSentences(substart, subend, sentences, splitedSpans, 2);
        MergeSentences(substart, subend, sentences, splitedSpans, 3);
        MergeSentences(substart, subend, sentences, splitedSpans, 4);
        MergeSentences(substart, subend, sentences, splitedSpans, 5);
        MergeSentences(substart, subend, sentences, splitedSpans, 6);
        MergeSentences(substart, subend, sentences, splitedSpans, 7);
        MergeSentences(substart, subend, sentences, splitedSpans, 8);
        MergeSentences(substart, subend, sentences, splitedSpans, 9);
        MergeSentences(substart, subend, sentences, splitedSpans, 10);
    }

    return splitedSpans;
}

From source file:com.att.aro.datacollector.ioscollector.utilities.AppSigningHelper.java

private void signFiles(String certName, String id, String[] filesToSign) throws IOSAppException {
    String line = extProcRunner.executeCmd(Commands.signApp(certName, provProfile.getCodesignId()));
    verifyFileSigned(line, VO_APP_FILE);
    line = extProcRunner.executeCmd(Commands.signFrameworkFiles(certName, provProfile.getCodesignId()));
    String numOfFiles = extProcRunner
            .executeCmd(Commands.getNumOfFilesInDir(APP_PATH + Util.FILE_SEPARATOR + "Frameworks"));
    if (numOfFiles.matches("^[0-9]*$")) {
        int numFiles = Integer.valueOf(numOfFiles);
        int numFilesReplaced = StringUtils.countMatches(line, SIGNATURE_REPLACED_MSG);
        if (numFiles != numFilesReplaced) {
            throw new IOSAppException(ErrorCodeRegistry.getFileSigningError());
        }//from w ww  . j av  a 2 s  .c  o m
    }

    for (String fileToSign : filesToSign) {
        line = extProcRunner.executeCmd(Commands.signFile(certName, provProfile.getCodesignId(), fileToSign));
        verifyFileSigned(line, fileToSign);
    }

    line = extProcRunner.executeCmd(Commands.signApp(certName, provProfile.getCodesignId()));
    verifyFileSigned(line, VO_APP_FILE);
}

From source file:bs.ws1.dm.itg.App.java

private static boolean csvStructureValid(String csvFile, String dataClass, int nofFieldsReference)
        throws FileNotFoundException {
    boolean structureValid = false;
    List<String> lines = readFileIntoStringList(csvFile);

    int lineCounter = 1, nofFields = 1, invalidRecords = 0;
    for (String line : lines) {
        nofFields = StringUtils.countMatches(line, "|") + 1;
        if (nofFieldsReference != nofFields) {
            // error case ....
            LOG.log(Level.SEVERE, dataClass + ": Line " + String.valueOf(lineCounter) + " - " + nofFields
                    + " fields -> expected=" + nofFieldsReference);
            //LOG.log(Level.SEVERE, line);
            invalidRecords++;/*from  w w  w  .j a v a2s .c om*/
        }
        lineCounter++;
        nofFields = 1;
    }

    LOG.log(Level.INFO, "\n\n########################## csv structure test###################################");
    if (invalidRecords < 1) {
        structureValid = true;
        LOG.log(Level.INFO, dataClass
                + ": CSV structure valid:\t\t".concat(String.valueOf(lines.size())).concat(" records"));
    } else {
        LOG.log(Level.INFO,
                dataClass + ": Total records:\t\t\t".concat(String.valueOf(lines.size())).concat(" lines"));
        LOG.log(Level.INFO, dataClass
                + ": Total invalid records:\t\t".concat(String.valueOf(invalidRecords)).concat(" lines"));
        LOG.log(Level.INFO, dataClass + ": Total valid records:\t\t"
                .concat(String.valueOf(lines.size() - invalidRecords)).concat(" lines"));
    }
    return structureValid;
}

From source file:com.temenos.interaction.core.rim.TestHeaderHelper.java

@Test
public void testEncodeQueryParametersWithMultivaluedMapContainingEmptyLists() {
    HashMap<String, List<String>> values = new MultivaluedMapImpl<String>();
    values.put("customerName", new ArrayList<String>());
    values.put("transaction", new ArrayList<String>());
    values.put("item", new ArrayList<String>(Arrays.asList(new String[] { "apple" })));
    values.put("money", new ArrayList<String>());
    MultivaluedMap<String, String> reinterpretedMap = new MultivaluedMapImpl<String>();
    reinterpretedMap.putAll(values);//from   www .  j av  a2 s .co m
    String queryParam = HeaderHelper.encodeMultivalueQueryParameters(reinterpretedMap);
    assertThat(queryParam, containsString("item=apple"));
    assertThat(StringUtils.countMatches(queryParam, "&"), equalTo(3));
}

From source file:com.hangum.tadpole.rdb.core.editors.main.composite.QueryHistoryComposite.java

/**
 * add grid row/*from   w w  w  .  ja v  a 2s.  c om*/
 * 
 * @param reqResultDAO
 */
private void addRowData(RequestResultDAO reqResultDAO) {
    GridItem item = new GridItem(gridSQLHistory, SWT.V_SCROLL | SWT.H_SCROLL);

    String strSQL = StringUtils.strip(reqResultDAO.getStrSQLText());
    int intLine = StringUtils.countMatches(strSQL, "\n"); //$NON-NLS-1$
    if (intLine >= 1) {
        int height = (intLine + 1) * 24;
        if (height > 100)
            item.setHeight(100);
        else
            item.setHeight(height);
    }

    item.setText(0, "" + gridSQLHistory.getRootItemCount()); //$NON-NLS-1$
    item.setText(1, Utils.dateToStr(reqResultDAO.getStartDateExecute()));
    item.setText(2, Utils.convLineToHtml(strSQL));
    item.setToolTipText(2, strSQL);

    item.setText(3,
            "" + ((reqResultDAO.getEndDateExecute().getTime() - reqResultDAO.getStartDateExecute().getTime()) //$NON-NLS-1$
                    / 1000f));
    item.setText(4, "" + reqResultDAO.getRows()); //$NON-NLS-1$
    item.setText(5, reqResultDAO.getResult());

    item.setText(6, Utils.convLineToHtml(reqResultDAO.getMesssage()));
    item.setToolTipText(6, reqResultDAO.getMesssage());

    if ("F".equals(reqResultDAO.getResult())) { //$NON-NLS-1$
        item.setBackground(SWTResourceManager.getColor(240, 180, 167));
    }
}