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

The following are Jave code examples for showing how to use splitByWholeSeparatorPreserveAllTokens() 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: j1st-mqtt   File: Topics.java   View Source Code Vote up 6 votes
/**
 * Validate the topic name, add EMPTY and END, return as a List of levels
 * No wildcard allowed!
 *
 * @param topicName Topic Name
 * @return List of levels
 */
public static List<String> sanitizeTopicName(String topicName) {
    if (StringUtils.isEmpty(topicName)) throw new IllegalArgumentException("Empty topic name");
    if (topicName.contains("+")) throw new IllegalArgumentException("Topic name contains wildcard");
    if (topicName.contains("#")) throw new IllegalArgumentException("Topic name contains wildcard");

    List<String> levels = new ArrayList<>();

    if (topicName.startsWith("/")) topicName = EMPTY + topicName;
    if (topicName.endsWith("/")) topicName = topicName + EMPTY;

    String[] tokens = StringUtils.splitByWholeSeparatorPreserveAllTokens(topicName, "/");
    for (String token : tokens) {
        levels.add(StringUtils.isNotEmpty(token) ? token : EMPTY);
    }

    if (!topicName.endsWith(END)) levels.add(END);

    return levels;
}
 
Example 2
Project: j1st-mqtt   File: Topics.java   View Source Code Vote up 6 votes
/**
 * Validate the topic filter, add EMPTY and END, return as a List of levels
 *
 * @param topicFilter Topic Filter
 * @return List of levels
 */
public static List<String> sanitizeTopicFilter(String topicFilter) {
    if (StringUtils.isEmpty(topicFilter)) throw new IllegalArgumentException("Empty topic filer");
    if (!topicFilter.contains("+") && !topicFilter.contains("#"))
        throw new IllegalArgumentException("Topic filter does not contain wildcard");

    List<String> levels = new ArrayList<>();

    if (topicFilter.startsWith("/")) topicFilter = EMPTY + topicFilter;
    if (topicFilter.endsWith("/")) topicFilter = topicFilter + EMPTY;

    String[] tokens = StringUtils.splitByWholeSeparatorPreserveAllTokens(topicFilter, "/");
    for (int i = 0; i < tokens.length; i++) {
        if (tokens[i].contains("+") && !tokens[i].equals("+"))
            throw new IllegalArgumentException("Illegal topic filter: " + topicFilter);
        if (tokens[i].contains("#") && !tokens[i].equals("#"))
            throw new IllegalArgumentException("Illegal topic filter: " + topicFilter);
        if (tokens[i].equals("#") && !(i == tokens.length - 1 || (i == tokens.length - 2 && END.equals(tokens[i + 1]))))
            throw new IllegalArgumentException("Illegal topic filter: " + topicFilter);
        levels.add(StringUtils.isNotEmpty(tokens[i]) ? tokens[i] : EMPTY);
    }

    if (!topicFilter.endsWith(END)) levels.add(END);

    return levels;
}
 
Example 3
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);
    }
}