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

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

Introduction

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

Prototype

public static String[] splitPreserveAllTokens(final String str, final 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:org.structr.console.tabcompletion.AbstractTabCompletionProvider.java

protected String getToken(final String line, final String separators) {

    final String[] parts = StringUtils.splitPreserveAllTokens(line, separators);
    if (parts.length > 0) {

        return parts[parts.length - 1];
    }//from ww  w  .  j  ava2  s.c  o m

    return "";
}

From source file:org.wikimedia.analytics.refinery.core.Webrequest.java

/**
 * Classifies a referer//from   w w w .  j a v  a 2 s.  com
 *
 * @param url The referer url to classify
 * @return RefererClassification
 */
public String classifyReferer(String url) {
    if (url == null || url.isEmpty() || url.equals("-")) {
        return Referer.NONE.getRefLabel();
    }

    String[] urlParts = StringUtils.splitPreserveAllTokens(url, '/');
    if (urlParts == null || urlParts.length < 3) {
        return Referer.UNKNOWN.getRefLabel();
    }

    if (!urlParts[0].equals("http:") && !urlParts[0].equals("https:")) {
        return Referer.UNKNOWN.getRefLabel();
    }

    if (!urlParts[1].isEmpty()) {
        return Referer.UNKNOWN.getRefLabel();
    }

    String[] domainParts = StringUtils.splitPreserveAllTokens(urlParts[2], '.');

    if (domainParts == null || domainParts.length < 2) {
        return Referer.UNKNOWN.getRefLabel();
    }

    if (domainParts[domainParts.length - 1].equals("org")) {
        switch (domainParts[domainParts.length - 2]) {
        case "":
            return Referer.UNKNOWN.getRefLabel();
        case "mediawiki":
        case "wikibooks":
        case "wikidata":
        case "wikinews":
        case "wikimedia":
        case "wikimediafoundation":
        case "wikipedia":
        case "wikiquote":
        case "wikisource":
        case "wikiversity":
        case "wikivoyage":
        case "wiktionary":
            return Referer.INTERNAL.getRefLabel();
        }
    }
    return Referer.EXTERNAL.getRefLabel();
}

From source file:org.zanata.adapter.po.PoWriter2.java

private static void copyMetadataToMessage(PotEntryHeader data, SimpleComment simpleComment, Message message) {
    if (data != null) {
        String context = data.getContext();
        if (context != null)
            message.setMsgctxt(context);
        for (String flag : data.getFlags()) {
            message.addFormat(flag);// ww w.ja v  a2 s. co  m
        }
        for (String ref : data.getReferences()) {
            message.addSourceReference(ref);
        }
    }
    if (simpleComment != null) {
        String[] comments = StringUtils.splitPreserveAllTokens(simpleComment.getValue(), "\n");
        if (!(comments.length == 1 && comments[0].isEmpty())) {
            for (String comment : comments) {
                message.addExtractedComment(comment);
            }
        }
    }
}

From source file:se.trixon.jota.client.ui.editor.module.task.TaskExcludePanel.java

@Override
public void loadTask(Task task) {
    ExcludeSection excludeSection = task.getExcludeSection();

    externalFilePanel.setSelected(excludeSection.isManualFileUsed());
    externalFilePanel.setPath(excludeSection.getManualFilePath());
    externalFilePanel.setEnabled(externalFilePanel.isSelected());

    String[] options = StringUtils.splitPreserveAllTokens(excludeSection.getOptions(), " ");
    for (String option : options) {
        for (Object object : dualListPanel.getAvailableListPanel().getModel().toArray()) {
            OptionHandler optionHandler = (OptionHandler) object;
            if (StringUtils.equals(option, optionHandler.getArg())) {
                dualListPanel.getSelectedListPanel().getModel().addElement(object);
                dualListPanel.getAvailableListPanel().getModel().removeElement(object);
                break;
            }//from   www  . ja v  a 2  s  .  c om
        }
    }

    dualListPanel.getSelectedListPanel().updateModel();
    dualListPanel.getAvailableListPanel().updateModel();
}

From source file:se.trixon.jota.client.ui.editor.module.task.TaskOptionsPanel.java

@Override
public void loadTask(Task task) {
    OptionSection optionSection = task.getOptionSection();
    String[] options = StringUtils.splitPreserveAllTokens(optionSection.getOptions(), " ");
    for (String option : options) {
        for (Object object : dualListPanel.getAvailableListPanel().getModel().toArray()) {
            OptionHandler optionHandler = (OptionHandler) object;
            if (option.contains("=")) {
                String[] elements = StringUtils.split(option, "=", 2);
                if (StringUtils.equals(elements[0], StringUtils.split(optionHandler.getArg(), "=", 2)[0])) {
                    optionHandler.setDynamicArg(elements[1]);
                    dualListPanel.getSelectedListPanel().getModel().addElement(object);
                    dualListPanel.getAvailableListPanel().getModel().removeElement(object);
                }/*from   w ww  . ja v  a 2 s. c o  m*/
            } else if (StringUtils.equals(option, optionHandler.getArg())) {
                dualListPanel.getSelectedListPanel().getModel().addElement(object);
                dualListPanel.getAvailableListPanel().getModel().removeElement(object);
                break;
            }
        }
    }

    dualListPanel.getSelectedListPanel().updateModel();
    dualListPanel.getAvailableListPanel().updateModel();
}

From source file:se.trixon.pacoma.ui.PropertiesPanel.java

private void init() {
    mDimensions.add(new Dimension(0, 0));
    mDimensions.add(new Dimension(800, 600));
    mDimensions.add(new Dimension(1600, 1200));
    mDimensions.add(new Dimension(3508, 2480));
    mDimensions.add(new Dimension(2480, 3508));
    mDimensions.add(new Dimension(4960, 3508));
    mDimensions.add(new Dimension(3508, 4960));
    mDimensions.add(new Dimension(3300, 2550));
    mDimensions.add(new Dimension(2550, 3300));

    templateComboBox.setModel(new DefaultComboBoxModel<>(
            StringUtils.splitPreserveAllTokens(mBundleUI.getString("image_template"), ";")));
    widthSpinner.setEditor(new JSpinner.NumberEditor(widthSpinner, "#"));
    heightSpinner.setEditor(new JSpinner.NumberEditor(heightSpinner, "#"));

    widthSpinner.setValue(mCollage.getWidth());
    heightSpinner.setValue(mCollage.getHeight());

    colorPanel.setColor(mCollage.getBorderColor());
    thicknessSpinner.setValue(mCollage.getBorderThickness() * 100);
}

From source file:se.trixon.toolbox.geotagger.GeotaggerTopComponent.java

private void openFileCsv(File file) throws IOException {
    List<String> lines = FileUtils.readLines(file);

    lines.stream().forEach((line) -> {
        String[] elements = StringUtils.splitPreserveAllTokens(line, ";");
        if (elements.length < 3 || elements[0].isEmpty() || elements[1].isEmpty() || elements[2].isEmpty()) {
            logAppend(String.format(" - %s: %s", mBundle.getString("invalidGeotag"), line));
        } else {/*from  w ww . ja v  a2  s .  c o m*/
            String name = elements[GeotagTableModel.COLUMN_NAME];
            double latitude = NumberUtils.toDouble(elements[GeotagTableModel.COLUMN_LATITUDE]);
            double longitude = NumberUtils.toDouble(elements[GeotagTableModel.COLUMN_LONGITUDE]);
            double altitude = 0;

            if (elements.length > GeotagTableModel.COLUMN_ALTITUDE) {
                altitude = NumberUtils.toDouble(elements[GeotagTableModel.COLUMN_ALTITUDE]);
            }

            Geotag geotag = new Geotag(name, latitude, longitude, altitude);
            mTableModel.addRow(geotag);
        }
    });
}

From source file:ubic.basecode.io.reader.AbstractMatrixReader.java

/**
 * @param dis// w  ww.  ja  v  a 2s. c  o m
 * @param skipColumns how many data columns shoul be ignored
 * @return
 * @throws IOException
 */
protected List<String> readHeader(BufferedReader dis, int skipColumns) throws IOException {
    List<String> headerVec = new Vector<String>();
    String header = null;

    /*
     * Read past comments.
     */
    while ((header = dis.readLine()) != null) {
        if (header.startsWith("#") || header.startsWith("!") || StringUtils.isBlank(header)) {
            continue;
        }
        break;
    }

    if (header == null)
        return headerVec;

    if (header.startsWith("\t"))
        header = "c" + header;

    String[] tokens = StringUtils.splitPreserveAllTokens(header, "\t");
    // delims.

    String previousToken = "";
    int columnNumber = 0;

    for (int i = 0; i < tokens.length; i++) {
        String s = StringUtils.strip(tokens[i], " ");
        boolean missing = false;

        if (s.compareTo("\t") == 0) {

            if (previousToken.compareTo("\t") == 0) { /* two tabs in a row */
                missing = true;
            } else if (i == tokens.length - 1) { // at end of line.
                missing = true;
            } else {
                previousToken = s;
                continue;
            }
        } else if (StringUtils.isBlank(s)) {
            missing = true;
        }

        if (missing) {
            throw new IOException("Missing values are not allowed in the header (column " + columnNumber
                    + " at '" + header + "')");
        }
        if (columnNumber > 0) {

            if (skipColumns > 0 && columnNumber <= skipColumns) {

                // ignore, but count it.
            } else {
                headerVec.add(s);
            }
        } else {
            // corner string.
        }
        columnNumber++;

        previousToken = s;
    }

    return headerVec;

}

From source file:ubic.basecode.io.reader.DoubleMatrixReader.java

/**
 * @param row// w w  w .  j ava2 s. c  o  m
 * @param rowNames
 * @param MTemp
 * @param wantedRowNames
 * @param skipColumns the number of columns after the first to ignore (for example, Gemma output that includes gene
 *        information as well as numeric data)
 * @return
 * @throws IOException
 */
private String parseRow(String row, Collection<String> rowNames, List<DoubleArrayList> MTemp,
        Collection<String> wantedRowNames, int skipColumns) throws IOException {

    if (row.startsWith("#") || row.startsWith("!")) {
        return null;
    }

    String[] tokens = StringUtils.splitPreserveAllTokens(row, "\t");

    DoubleArrayList rowTemp = new DoubleArrayList();
    int columnNumber = 0;
    String previousToken = "";
    String currentRowName = null;
    for (int i = 0; i < tokens.length; i++) {
        String tok = tokens[i];
        boolean missing = false;

        if (tok.compareTo("\t") == 0) {
            /* two tabs in a row */
            if (previousToken.compareTo("\t") == 0) {
                missing = true;
            } else if (i == tokens.length - 1) { // at end of line.
                missing = true;
            } else {
                previousToken = tok;
                continue;
            }
        } else if (StringUtils.isBlank(tok) || tok.compareTo("NaN") == 0 || tok.compareTo("NA") == 0) {
            missing = true;
        }

        if (columnNumber > 0) {

            if (skipColumns > 0 && columnNumber <= skipColumns) {
                // skip.
            } else if (missing) {
                rowTemp.add(Double.NaN);
            } else {
                try {
                    /*
                     * NumberFormat.parse thinks things like 9101001_at are okay. Try to catch such cases. Note that
                     * we can't use Double.parseDouble because that doesn't seem to handle locale-specific number
                     * formats like european decimals (0,001 etc.)
                     */
                    // if ( tok.matches( ".*[a-zA-Z_=].*" ) ) {
                    // throw new NumberFormatException( "Unexpected non-numeric value found in column "
                    // + columnNumber + ": " + tok );
                    // }
                    rowTemp.add(nf.parse(tok.toUpperCase()).doubleValue());
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            // First field is the row label.

            if (missing) {
                throw new IOException(
                        "Missing values not allowed for row labels (" + StringUtils.abbreviate(row, 20) + ")");
            }

            currentRowName = tok;

            // Skip rows. Return the row name anyway.
            if (wantedRowNames != null && !wantedRowNames.contains(currentRowName)) {
                return currentRowName;
            }

            rowNames.add(currentRowName);
        }

        columnNumber++;
        previousToken = tok;
    } // end while (st.hasMoreTokens())
      // done parsing one row -- no more tokens

    if (rowTemp.size() > numHeadings) {
        throw new IOException("Too many values (" + rowTemp.size() + ") in row  (based on headings count of "
                + numHeadings + ")");
    }

    MTemp.add(rowTemp);
    return currentRowName;

}

From source file:ubic.gemma.core.analysis.service.ArrayDesignAnnotationServiceImpl.java

/**
 * This tries to read one of the annotation files (noparents, bioprocess or regular) to get the gene information -
 * GO annotations are not part of the result.
 *
 * @param  arrayDesign array design/*from  w ww.j a  v  a2s . co m*/
 * @return             Map of composite sequence ids to an array of delimited strings: [probe name,genes symbol,
 *                     gene Name,
 *                     gemma gene id, ncbi id] for a given probe id. format of string is geneSymbol then geneNames
 *                     same as found
 *                     in annotation file.
 */
public static Map<Long, String[]> readAnnotationFileAsString(ArrayDesign arrayDesign) {
    Map<Long, String[]> results = new HashMap<>();
    File f = new File(ArrayDesignAnnotationService.ANNOT_DATA_DIR
            + ArrayDesignAnnotationServiceImpl.mungeFileName(arrayDesign.getShortName())
            + ArrayDesignAnnotationService.STANDARD_FILE_SUFFIX
            + ArrayDesignAnnotationService.ANNOTATION_FILE_SUFFIX);
    if (!f.canRead()) {
        /*
         * Look for more files.
         */
        f = new File(ArrayDesignAnnotationService.ANNOT_DATA_DIR
                + ArrayDesignAnnotationServiceImpl.mungeFileName(arrayDesign.getShortName())
                + ArrayDesignAnnotationService.NO_PARENTS_FILE_SUFFIX
                + ArrayDesignAnnotationService.ANNOTATION_FILE_SUFFIX);

        if (!f.canRead()) {
            f = new File(ArrayDesignAnnotationService.ANNOT_DATA_DIR
                    + ArrayDesignAnnotationServiceImpl.mungeFileName(arrayDesign.getShortName())
                    + ArrayDesignAnnotationService.BIO_PROCESS_FILE_SUFFIX
                    + ArrayDesignAnnotationService.ANNOTATION_FILE_SUFFIX);
        }

        if (!f.canRead()) {
            ArrayDesignAnnotationServiceImpl.log.info(
                    "Gene annotations are not available in " + ArrayDesignAnnotationService.ANNOT_DATA_DIR);
            return results;
        }
    }

    Map<String, Long> probeNameToId = new HashMap<>();

    int FIELDS_PER_GENE = 5; // used to be 3, now is 5;

    boolean warned = false;
    for (CompositeSequence cs : arrayDesign.getCompositeSequences()) {
        results.put(cs.getId(), new String[FIELDS_PER_GENE]);
        if (probeNameToId.containsKey(cs.getName()) && !warned) {
            ArrayDesignAnnotationServiceImpl.log.warn("Duplicate probe name: " + cs.getName() + " for "
                    + arrayDesign + " (further warnings suppressed)");
            warned = true;
        }
        probeNameToId.put(cs.getName(), cs.getId());
    }

    try (InputStream is = FileTools.getInputStreamFromPlainOrCompressedFile(f.getAbsolutePath());
            BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
        ArrayDesignAnnotationServiceImpl.log.info("Reading annotations from: " + f);

        String line;

        while ((line = br.readLine()) != null) {
            if (StringUtils.isBlank(line)
                    || line.startsWith(ArrayDesignAnnotationServiceImpl.COMMENT_CHARACTER)) {
                continue;
            }
            String[] fields = StringUtils.splitPreserveAllTokens(line, '\t');

            if (fields.length < 3)
                continue; // means there are no gene annotations.

            String probeName = fields[0];

            if (!probeNameToId.containsKey(probeName))
                continue;
            Long probeId = probeNameToId.get(probeName);

            results.get(probeId)[0] = probeName; // Probe Name (redundant!)
            results.get(probeId)[1] = fields[1]; // Gene Symbol(s)
            results.get(probeId)[2] = fields[2]; // Gene Name

            // fields[3] is the GO annotations, we skip that.

            if (fields.length > 4) {
                results.get(probeId)[3] = fields[4]; // Gemma Id
            }

            if (fields.length > 5) {
                results.get(probeId)[4] = fields[5]; // NCBI id.
            }

        }

        is.close();

        return results;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}