FileDocCategorySizeDatePackage
QuotedStringTokenizer.javaAPI DocGlassfish v2 API5501Fri May 04 22:25:58 BST 2007com.sun.enterprise.admin.util

QuotedStringTokenizer

public class QuotedStringTokenizer extends Object

Fields Summary
private final char[]
ca
private String
delimiters
private final int
numTokens
private int
curToken
private final CharIterator
iterator
Constructors Summary
public QuotedStringTokenizer(String s)


      
    
        this(s, null);
    
public QuotedStringTokenizer(String s, String delim)

        if (null == s)
        {
            throw new IllegalArgumentException("null param");
        }
        ca = s.toCharArray();
        if (delim != null && delim.length() > 0)
        {
            delimiters = delim;
        }
        numTokens = _countTokens();
        iterator = new CharIterator(ca);
    
Methods Summary
private int_countTokens()

        int     tokens = 0;
        boolean bQuote = false;
        boolean bEscaped = false;
        final   CharIterator it = new CharIterator(ca);
        char    c;

        while ((c = it.next()) != CharIterator.EOF)
        {
            char next = it.peekNext();
            if (!isDelimiter(c) && !bEscaped)
            {
                if (c == '\"")
                    bQuote = !bQuote;
                if (next == CharIterator.EOF || (isDelimiter(next) && !bQuote))
                    tokens++;
            }
            else if (next == CharIterator.EOF && bQuote) //eg :- "\" "
                tokens++;
            if(c=='\\")
                bEscaped = !bEscaped;
            else
                bEscaped = false;
        }
        return tokens;
    
public intcountTokens()

        return numTokens;
    
public booleanhasMoreTokens()

        return curToken < numTokens;
    
booleanisDelimiter(char c)

        return delimiters.indexOf(c) >= 0;
    
public java.lang.StringnextToken()

        if (curToken == numTokens)
            throw new NoSuchElementException();
        final StringBuffer sb = new StringBuffer();
        boolean bQuote = false;
        boolean bEscaped = false;
        char c;
        while ((c = iterator.next()) != CharIterator.EOF)
        {
            boolean isDelimiter = isDelimiter(c);
            if (!isDelimiter && !bEscaped)
            {
                sb.append(c);
                if (c == '\"")
                    bQuote = !bQuote;
                char next = iterator.peekNext();
                if (next == CharIterator.EOF || (isDelimiter(next) && !bQuote))
                    break;
            }
            else if (bQuote || bEscaped)
            {
                sb.append(c);
            }
            if(c=='\\")
                bEscaped = !bEscaped;
            else
                bEscaped = false;
        }
        curToken++;
        return sb.toString();