Java String Tokenize escapedTokens(String s, char separator)

Here you can find the source of escapedTokens(String s, char separator)

Description

Tokenize a String using the specified separator character and the backslash as an escape character (see OGC WFS 1.1.0 14.2.2).

License

Open Source License

Parameter

Parameter Description
s the String to parse
separator the character that separates tokens

Return

list of tokens

Declaration

public static List<String> escapedTokens(String s, char separator) 

Method Source Code

//package com.java2s;
/* Copyright (c) 2001 - 2007 TOPP - www.openplans.org.  All rights reserved.
 * This code is licensed under the GPL 2.0 license, availible at the root
 * application directory./*  w  ww  . j  a va2  s . co m*/
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Tokenize a String using the specified separator character and the backslash as an escape 
     * character (see OGC WFS 1.1.0 14.2.2).  Escape characters within the tokens are not resolved. 
     * 
     *  @param s the String to parse
     *  @param separator the character that separates tokens
     *  
     *  @return list of tokens
     */
    public static List<String> escapedTokens(String s, char separator) {
        if (s == null) {
            throw new IllegalArgumentException(
                    "The String to parse may not be null.");
        }
        if (separator == '\\') {
            throw new IllegalArgumentException(
                    "The separator may not be a backslash.");
        }
        List<String> ret = new ArrayList<String>();
        StringBuilder sb = new StringBuilder();
        boolean escaped = false;

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == separator && !escaped) {
                ret.add(sb.toString());
                sb.setLength(0);
            } else {
                if (escaped) {
                    escaped = false;
                    sb.append('\\');
                    sb.append(c);
                } else if (c == '\\') {
                    escaped = true;
                } else {
                    sb.append(c);
                }
            }
        }
        if (escaped) {
            throw new IllegalStateException(
                    "The specified String ends with an incomplete escape sequence.");
        }
        ret.add(sb.toString());
        return ret;
    }
}

Related

  1. conservativeTokenize(String text)
  2. countCommonTokens(String string1, String string2)
  3. extractTokens(String strStartToken, String strEndToken, String strExpression)
  4. extractTokens(String text, String delim)
  5. getAllTokens(final String string)
  6. getCommandTokens(String commandString)