Java Code Examples for org.apache.commons.lang3.StringUtils.splitByWholeSeparator()

The following are Jave code examples for showing how to use splitByWholeSeparator() of the org.apache.commons.lang3.StringUtils class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: vscrawler   File: SplitByWholeSeparator.java   View Source Code Vote up 5 votes
@Override
protected String[] split(String str, String separatorChars, int max, boolean preserveAllTokens) {
    if (preserveAllTokens) {
        return StringUtils.splitByWholeSeparatorPreserveAllTokens(str, separatorChars, max);
    } else {
        return StringUtils.splitByWholeSeparator(str, separatorChars, max);
    }
}
 
Example 2
Project: remittanceparse   File: RemittancePdf.java   View Source Code Vote up 4 votes
private void handleDate(File tempTsvFileForDate) throws IOException, ParserException {

        String date=null;
        try(BufferedReader reader=new BufferedReader(new FileReader(tempTsvFileForDate)))
        {
            for(String line=reader.readLine();line!=null;line=reader.readLine())
            {
                line=line.trim();
                if(!StringUtils.startsWithIgnoreCase(line,"Date"))
                    continue;

                final String[] split = StringUtils.splitByWholeSeparator(line, " ");
                if(split.length!=2)
                    throw new ParserException("Could not parse date field: "+line);

                date=split[1];
                break;
            }
        }

        if(date==null)
            throw new ParserException("Never found date field");

        //start guessing at the date format
        boolean isEuroDate=isEuroDate(date);
        boolean isUSDate=isUSDate(date);

        if(!isEuroDate && !isUSDate)
            throw new ParserException("Could not parse date field: "+date);

        if(isEuroDate && !isUSDate) {
            dateParser = Format.euroDateFormatter;
            return;
        }

        if(!isEuroDate) {
            dateParser = Format.usDateFormatter;
            return;
        }

        //hmmm.... date is valid in both formats. Use the filename instead

        try {
            final String[] array = StringUtils.splitByWholeSeparator(inputPdfFile.getName(), "-");
            int year = Integer.parseInt(array[0]);
            int month = Integer.parseInt(array[1]);
            if (year < 2017 || month <= 6)
                dateParser = Format.euroDateFormatter;
            else
                dateParser = Format.usDateFormatter;

            remittanceDate=dateParser.parse(date);

        }
        catch (Throwable t)
        {
            throw new ParserException("Can not determine date format to use.",t);
        }

    }
 
Example 3
Project: SmartFramework   File: StringUtil.java   View Source Code Vote up 4 votes
/**
 * 分割固定格式的字符串
 */
public static String[] splitString(String str, String separator) {
    return StringUtils.splitByWholeSeparator(str, separator);
}
 
Example 4
Project: obevo   File: TextMarkupDocumentReaderOld.java   View Source Code Vote up 4 votes
private ImmutableList<TextMarkupDocumentSection> parseString(String text, MutableList<String> elementsToCheck, boolean recurse,
        String elementPrefix) {
    MutableList<TextMarkupDocumentSection> sections = Lists.mutable.empty();
    while (true) {
        int earliestIndex = Integer.MAX_VALUE;

        for (String firstLevelElement : elementsToCheck) {
            int index = text.indexOf(elementPrefix + " " + firstLevelElement, 1);
            if (index != -1 && index < earliestIndex) {
                earliestIndex = index;
            }
        }

        if (earliestIndex == Integer.MAX_VALUE) {
            sections.add(new TextMarkupDocumentSection(null, text));
            break;
        } else {
            sections.add(new TextMarkupDocumentSection(null, text.substring(0, earliestIndex)));
            text = text.substring(earliestIndex);
        }
    }
    for (TextMarkupDocumentSection section : sections) {
        MutableMap<String, String> attrs = Maps.mutable.empty();
        MutableSet<String> toggles = Sets.mutable.empty();
        String content = StringUtils.chomp(section.getContent());

        String[] contents = content.split("\\r?\\n", 2);
        String firstLine = contents[0];

        for (String elementToCheck : elementsToCheck) {
            if (firstLine.startsWith(elementPrefix + " " + elementToCheck)) {
                section.setName(elementToCheck);
                String[] args = StringUtils.splitByWholeSeparator(firstLine, " ");
                for (String arg : args) {
                    if (arg.contains("=")) {
                        String[] attr = arg.split("=");
                        if (attr.length > 2) {
                            throw new IllegalArgumentException("Cannot mark = multiple times in a parameter - "
                                    + firstLine);
                        }
                        String attrVal = attr[1];
                        if (attrVal.startsWith("\"") && attrVal.endsWith("\"")) {
                            attrVal = attrVal.substring(1, attrVal.length() - 1);
                        }
                        attrs.put(attr[0], attrVal);
                    } else {
                        toggles.add(arg);
                    }
                }
                if (contents.length > 1) {
                    content = contents[1];
                } else {
                    content = null;
                }
            }
        }
        section.setAttrs(attrs.toImmutable());
        section.setToggles(toggles.toImmutable());

        if (!recurse) {
            section.setContent(content);
        } else if (content != null) {
            ImmutableList<TextMarkupDocumentSection> subsections = this.parseString(content, this.secondLevelElements, false, "//");
            if (subsections.size() == 1) {
                section.setContent(content);
            } else {
                section.setContent(subsections.get(0).getContent());
                section.setSubsections(subsections.subList(1, subsections.size()));
            }
        } else {
            section.setContent(null);
        }
    }

    return sections.toImmutable();
}
 
Example 5
Project: obevo   File: TextMarkupDocumentReader.java   View Source Code Vote up 4 votes
Pair<ImmutableMap<String, String>, ImmutableSet<String>> parseAttrsAndToggles(String line) {
    MutableMap<String, String> attrs = Maps.mutable.empty();
    MutableSet<String> toggles = Sets.mutable.empty();

    if (!legacyMode) {
        List<Token> tokens = TextMarkupParser.parseTokens(line);
        Token curToken = !tokens.isEmpty() ? tokens.get(0) : null;
        while (curToken != null && curToken.kind != TextMarkupLineSyntaxParserConstants.EOF) {
            switch (curToken.kind) {
            case TextMarkupLineSyntaxParserConstants.WHITESPACE:
                // skip whitespace if encountered
                break;
            case TextMarkupLineSyntaxParserConstants.QUOTED_LITERAL:
            case TextMarkupLineSyntaxParserConstants.STRING_LITERAL:
                // let's check if this is a toggle or an attribute
                if (curToken.next.kind == TextMarkupLineSyntaxParserConstants.ASSIGN) {
                    Token keyToken = curToken;
                    curToken = curToken.next;  // to ASSIGN
                    curToken = curToken.next;  // to the following token
                    switch (curToken.kind) {
                    case TextMarkupLineSyntaxParserConstants.QUOTED_LITERAL:
                    case TextMarkupLineSyntaxParserConstants.STRING_LITERAL:
                        // in this case, we have an attribute value
                        String value = curToken.image;
                        if (value.charAt(0) == '"' && value.charAt(value.length() - 1) == '"') {
                            value = curToken.image.substring(1, curToken.image.length() - 1);
                        }
                        value = value.replaceAll("\\\\\"", "\"");
                        attrs.put(keyToken.image, value);
                        break;
                    case TextMarkupLineSyntaxParserConstants.WHITESPACE:
                    case TextMarkupLineSyntaxParserConstants.EOF:
                        // in this case, we will assume a blank value
                        attrs.put(keyToken.image, "");
                        break;
                    case TextMarkupLineSyntaxParserConstants.ASSIGN:
                    default:
                        throw new IllegalStateException("Not allowed here");
                    }
                } else {
                    toggles.add(curToken.image);
                }
                break;
            case TextMarkupLineSyntaxParserConstants.ASSIGN:
                toggles.add(curToken.image);
                break;
            case TextMarkupLineSyntaxParserConstants.EOF:
            default:
                throw new IllegalStateException("Should not arise");
            }

            curToken = curToken.next;
        }
    } else {
        // keeping this mode for backwards-compatibility until we can guarantee all clients are fine without it
        // This way cannot handle spaces in quotes
        String[] args = StringUtils.splitByWholeSeparator(line, " ");

        for (String arg : args) {
            if (arg.contains("=")) {
                String[] attr = arg.split("=");
                if (attr.length > 2) {
                    throw new IllegalArgumentException("Cannot mark = multiple times in a parameter - " + line);
                }
                String attrVal = attr[1];
                if (attrVal.startsWith("\"") && attrVal.endsWith("\"")) {
                    attrVal = attrVal.substring(1, attrVal.length() - 1);
                }
                attrs.put(attr[0], attrVal);
            } else if (StringUtils.isNotBlank(arg)) {
                toggles.add(arg);
            }
        }
    }

    return Tuples.pair(attrs.toImmutable(), toggles.toImmutable());
}
 
Example 6
Project: bfmvc   File: StringUtil.java   View Source Code Vote up 2 votes
/**
 * 分割固定格式字符串
 *
 * @param str
 * @param separator
 * @return
 */
public static String[] splitString(String str, String separator) {
    return StringUtils.splitByWholeSeparator(str, separator);
}