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

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

Introduction

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

Prototype

public static String[] splitPreserveAllTokens(String str, String separatorChars) 

Source Link

Document

Splits the provided text into an array, separators specified, preserving all tokens, including empty tokens created by adjacent separators.

Usage

From source file:corner.util.VectorUtils.java

/**
 * ??String/*from  w ww  .  j  ava  2 s  .  c  o m*/
 * split,',',split?String[][""],?List
 * 
 * @param str
 * @param regex
 * @return
 */
public static List<String> VectorSplit(String str, String regex) {
    if (str == null || str.trim().length() <= 0) {
        return null;
    } else {

        return Arrays.asList(StringUtils.splitPreserveAllTokens(str, ","));
    }
}

From source file:com.npower.dm.model.TestGenerateModelXMLByCSV.java

public void testStringUtils() throws Exception {
    {//  ww  w  .j  a  v a  2  s  .  c o  m
        String line1 = "0,1,2,3,4,5,6,7,8,9";
        String[] result = StringUtils.splitPreserveAllTokens(line1, ',');
        assertEquals(10, result.length);
    }
    {
        String line1 = ",1,,3,,5,,7,,9";
        String[] result = StringUtils.splitPreserveAllTokens(line1, ',');
        assertEquals(10, result.length);
        assertEquals("", result[0]);
        assertEquals("1", result[1]);
        assertEquals("", result[2]);
        assertEquals("3", result[3]);
        assertEquals("", result[4]);
        assertEquals("5", result[5]);
        assertEquals("", result[6]);
        assertEquals("7", result[7]);
        assertEquals("", result[8]);
        assertEquals("9", result[9]);
    }

    {
        String src = "break_list_of_links_with_br_element_recommended ";
        String result = WordUtils.capitalize(src, new char[] { '_', ' ' });
        assertEquals("Break_List_Of_Links_With_Br_Element_Recommended ", result);
    }
}

From source file:com.mapr.db.utils.ImportCSV.java

public void readAndImportCSV(String path, String delimiter) {

    String dataLine = "";
    String[] dataList;//from w  ww . j  av a  2 s . c  o m
    String data = "";

    ArrayList<Object> values = new ArrayList<>();
    int countColumnsInData = 0;

    try {
        Scanner scan = new Scanner(new FileReader(path));

        while (scan.hasNextLine()) {
            countColumnsInData = 0;
            dataLine = scan.nextLine().trim();
            //System.out.println(dataLine);
            if (dataLine.endsWith(delimiter)) {
                dataLine = dataLine.substring(0, dataLine.length() - 1);
            }
            dataList = StringUtils.splitPreserveAllTokens(dataLine, delimiter);

            for (int i = 0; i < dataList.length; i++) {

                data = dataList[i];
                if (data.isEmpty()) {
                    if (valueTypesInSchema.get(i) == "String") {
                        data = "null";
                    } else {
                        data = "0";
                    }
                }

                switch (valueTypesInSchema.get(i).toLowerCase()) {
                case "int":
                case "integer":
                case "bigint":
                case "long":
                    values.add(countColumnsInData, Long.valueOf(data));
                    break;
                case "float":
                case "double":
                    values.add(countColumnsInData, Double.valueOf(data));
                    break;
                case "date":
                    values.add(countColumnsInData, Date.valueOf(data));
                    break;
                default:
                    values.add(countColumnsInData, String.valueOf(data));
                    break;
                }

                //System.out.println("Inserting " + values.get(countColumnsInData)
                //        + " into column " + columnNamesInSchema.get(countColumnsInData));

                countColumnsInData++;
            }
            insertDocument(values, countColumnsInData, maprdbTable, maprdbTablePath);
        }
        scan.close();
    } catch (Exception e) {
        System.out.println("Error importing text:\n\t" + dataLine + "\ninto\n\t" + maprdbTablePath);
        e.printStackTrace();
        System.exit(-1);
    }
}

From source file:corner.util.VectorUtilsTest.java

public void testVectorSplit() {
    List<String> strList = VectorUtils.VectorSplit(COMMAEND_ARR[0], ",");
    assertEquals(4, strList.size());//w ww  .ja  v  a 2 s  .c om
    assertEquals(strList.get(0).toString(), "");
    assertEquals(strList.get(1).toString(), "a");
    assertEquals(strList.get(2).toString(), "b");
    assertEquals(strList.get(3).toString(), "");

    List<String> strList1 = VectorUtils.VectorSplit(COMMAEND_ARR[1], ",");
    assertEquals(2, strList1.size());
    assertEquals(strList1.get(0).toString(), "");
    assertEquals(strList1.get(1).toString(), "");

    List<String> strList2 = VectorUtils.VectorSplit(COMMAEND_ARR[2], ",");
    assertEquals(4, strList2.size());
    assertEquals(strList2.get(0).toString(), "");
    assertEquals(strList2.get(1).toString(), "");

    List<String> strList3 = VectorUtils.VectorSplit(COMMAEND_ARR[3], ",");
    assertEquals(3, strList3.size());
    assertEquals(strList3.get(0).toString(), "a");
    assertEquals(strList3.get(1).toString(), "b");
    assertEquals(strList3.get(2).toString(), "");

    List<String> strList4 = VectorUtils.VectorSplit("1,2,,,,", ",");
    assertEquals(6, strList4.size());

    String[] list5 = StringUtils.splitPreserveAllTokens("1,2,,,,", ",");

    assertEquals(6, list5.length);
}

From source file:edu.cornell.med.icb.io.TsvToFromMap.java

/**
 * Given a line of text, convert it to a Map[columnName, value] of data.
 * @param line the line of data to convert to the the Map
 * @return the Map of data//from   www.  j ava  2  s .  c om
 * @throws IOException error converting the data, the number of columns found
 * in the data is incorrect given the number of columns expected in the TSV file
 */
public LinkedHashToMultiTypeMap<String> readDataToMap(final String line) throws IOException {
    if (line.startsWith("#")) {
        return null;
    }
    final String[] parts = StringUtils.splitPreserveAllTokens(line, '\t');
    if ((!lenientColumnCount) && (parts.length != numColumnHeaders)) {
        throw new IOException(
                String.format("Line should have %d columns but has %d", numColumnHeaders, parts.length));
    }
    final LinkedHashToMultiTypeMap<String> result = new LinkedHashToMultiTypeMap<String>();
    int i = 0;
    final int numActualParts = parts.length;
    for (final String columnHeader : columnHeaders) {
        if (lenientColumnCount && (i >= numActualParts)) {
            result.put(columnHeader, "");
        } else {
            result.put(columnHeader, parts[i]);
        }
        i++;
    }
    return result;
}

From source file:edu.ku.brc.specify.toycode.BugParse.java

/**
 * @param lines/* w  w  w.ja v a 2  s .c  o  m*/
 */
protected void createChart(final List<String> lines, final String engineer) {

    int[] mins = new int[lines.size() - 1];
    for (int index = 1; index < lines.size(); index++) {
        String line = lines.get(index);
        String[] values = StringUtils.splitPreserveAllTokens(line, ",");
        int inx = 0;
        while (inx < values.length && values[inx].equals("0")) {
            inx++;
        }
        mins[index - 1] = inx < values.length ? inx : Integer.MAX_VALUE;
        System.err.println(mins[index - 1]);
    }

    int startInx = Integer.MAX_VALUE;
    for (int min : mins) {
        startInx = Math.min(startInx, min);
        System.out.println(min + "  " + startInx);
    }

    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    String[] headers = StringUtils.split(lines.get(0), ",");
    int len = headers.length - startInx;
    System.out.println(headers.length + "  " + len);
    List<double[]> valArray = new Vector<double[]>();

    for (int i = 1; i < lines.size(); i++) {
        String[] values = StringUtils.splitPreserveAllTokens(lines.get(i), ",");
        double[] vals = new double[len];
        int inx = 0;
        double prev = -1;
        for (int j = startInx; j < headers.length; j++) {
            if (StringUtils.isNotEmpty(values[j])) {
                prev = Double.parseDouble(values[j]);
                vals[inx++] = prev;
            } else {
                vals[inx++] = 0.0;
            }
        }
        valArray.add(vals);
    }

    double[] vals = valArray.get(0);
    for (int i = 0; i < vals.length; i++) {
        dataset.addValue(vals[i], "Bugs", headers[i + startInx]);
    }
    vals = valArray.get(1);
    for (int i = 0; i < vals.length; i++) {
        dataset.addValue(vals[i], "Resolved", headers[i + startInx]);
    }

    vals = valArray.get(2);
    for (int i = 0; i < vals.length; i++) {
        dataset.addValue(vals[i], "Open", headers[i + startInx]);
    }

    JFreeChart chart = ChartFactory.createLineChart("Bugs - " + engineer, "Time", "Bugs", dataset,
            PlotOrientation.VERTICAL, true, true, false);
    final CategoryPlot plot = (CategoryPlot) chart.getPlot();
    //plot.setBackgroundPaint(Color.lightGray);
    //plot.setRangeGridlinePaint(Color.white);

    // customise the range axis...
    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    rangeAxis.setAutoRangeIncludesZero(false);
    rangeAxis.setAxisLineVisible(true);

    CategoryAxis catAxis = plot.getDomainAxis();
    catAxis.setAxisLineVisible(true);
    catAxis.setTickMarksVisible(true);

    ChartFrame frame = new ChartFrame("", chart, false);
    frame.setBackground(Color.WHITE);
    frame.setSize(500, 500);
    frame.setVisible(true);
}

From source file:com.seer.datacruncher.utils.generic.CommonUtils.java

/**
 * Splits <tt>str</tt> around matches of the delimiter character.
 *
 * @param   str//www .j  a va 2  s.  co m
 *          the string to split
 *
 * @param   delimiter
 *          the delimiting field string
 *
 * @param   chrDelim
 *          the delimiting character
 *
 * @return  the array of strings computed by splitting string
 *          around matches of the delimiter character.
 *
 **/
public static String[] fieldSplit(String str, String delimiter, char chrDelim) {
    if ((str.indexOf(chrDelim, 0)) < 0) {
        return StringUtils.splitPreserveAllTokens(str, delimiter);
    } else {
        ArrayList<String> list = new ArrayList<String>();
        String record;
        List<String> streamsList = Arrays.asList(StringUtils.splitPreserveAllTokens(str, delimiter));
        int fEnd;
        for (int i = 0; i < streamsList.size(); i++) {
            record = streamsList.get(i);
            if ((record.indexOf(chrDelim, 0)) < 0) {
                list.add(record);
            } else {
                if (record.startsWith(chrDelim + "")) { // check in start field
                    fEnd = record.indexOf(chrDelim, 1); // find end
                    if (fEnd < 0) { //not found
                        if ((i + 1) < streamsList.size()) {
                            streamsList.set(i + 1, record + delimiter + streamsList.get(i + 1));
                        } else {
                            list.add(record);
                        }
                    } else {
                        list.add(record);
                    }
                }
            }
        }
        int resultSize = list.size();
        String[] result = new String[resultSize];
        return list.subList(0, resultSize).toArray(result);
    }
}

From source file:com.mapr.db.utils.ImportCSV_MR.java

public DBDocument readAndImportCSV(String dataLine) {

    ArrayList<Object> values = new ArrayList<>();
    int countColumnsInData = 0;

    if (dataLine.endsWith(conf.get("delimiter"))) {
        dataLine = dataLine.substring(0, dataLine.length() - 1);
    }/*from   w  w  w  .j av  a  2  s  .  c om*/

    String[] dataList = StringUtils.splitPreserveAllTokens(dataLine, conf.get("delimiter"));

    for (int i = 0; i < dataList.length; i++) {

        logger.info("Processing column " + i + " off " + dataList.length);
        System.out.println("Processing column " + i + " off " + dataList.length);

        String data = dataList[i];

        logger.info("Processing data " + data);
        System.out.println("Processing data " + data);

        if (data.isEmpty()) {
            if (conf.getStrings("valueTypesInSchema")[i] == "String") {
                data = "null";
            } else {
                data = "0";
            }
        }

        printSchema();

        switch (conf.getStrings("valueTypesInSchema")[i].toLowerCase()) {
        case "int":
        case "integer":
        case "bigint":
        case "long":
            values.add(countColumnsInData, Long.valueOf(data));
            break;
        case "float":
        case "double":
            values.add(countColumnsInData, Double.valueOf(data));
            break;
        case "date":
            values.add(countColumnsInData, Date.valueOf(data));
            break;
        default:
            values.add(countColumnsInData, String.valueOf(data));
            break;
        }

        countColumnsInData++;
    }
    return insertDocument(values, countColumnsInData, conf.get("tablePath"));
}

From source file:de.awtools.grooocle.inout.InOutUtils.java

/**
 * Splits a text String into pieces. Example:
 * //from  ww w .j  a v a 2s .  c om
 * <pre>
 * Groovy:
 * def expected = [&quot;&quot;, &quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;&quot;]
 * assert expected == InOutUtils.split(&quot;|a|b|c|&quot;, &quot;|&quot;)
 * </pre>
 * 
 * @param _text
 *            The text for splitting.
 * 
 * @param _seperator
 *            The seperator.
 * 
 * @return The splitted string.
 */
public static String[] split(final String _text, final String _seperator) {
    return StringUtils.splitPreserveAllTokens(_text, _seperator);
}

From source file:edu.mayo.informatics.lexgrid.convert.directConversions.NCIThesaurusHistoryFileToSQL.java

private void loadSystemReleaseFile(URI filePath, boolean failOnAllErrors) throws Exception {
    BufferedReader reader = getReader(filePath);

    int lineNumber = 0;
    String line = reader.readLine();
    while (line != null) {
        // format of line is
        // releaseDate|releaseAgency|releaseURN|releaseId|basedOnRelease|entityDescription
        if (line.startsWith("#") || line.length() == 0) {
            line = reader.readLine();/*from  w w  w .j a  va  2 s  .  c  om*/
            continue;
        }

        String[] tokens = StringUtils.splitPreserveAllTokens(line, token_);

        SystemRelease systemRelease = new SystemRelease();
        try {
            systemRelease.setReleaseDate(new Timestamp(dateFormat_.parse(tokens[0]).getTime()));
        } catch (ParseException e) {
            md_.fatalAndThrowException("Invalid date on line " + lineNumber, e);
        }
        systemRelease.setReleaseAgency(tokens[1]);
        systemRelease.setReleaseURI(tokens[2]);
        systemRelease.setReleaseId(tokens[3]);
        systemRelease.setBasedOnRelease(tokens[4]);

        EntityDescription ed = new EntityDescription();

        ed.setContent(tokens[5]);
        systemRelease.setEntityDescription(ed);

        LexEvsServiceLocator.getInstance().getDatabaseServiceManager().getNciHistoryService()
                .insertSystemRelease(this.codingSchemeUri, systemRelease);

        lineNumber++;
        line = reader.readLine();
    }
}