Java String Tokenize getTokenArray(String sequence, String[] chain, boolean paired, boolean includeDelim)

Here you can find the source of getTokenArray(String sequence, String[] chain, boolean paired, boolean includeDelim)

Description

get Token Array

License

Open Source License

Declaration

public static String[] getTokenArray(String sequence, String[] chain, boolean paired, boolean includeDelim)
            throws IllegalArgumentException 

Method Source Code

//package com.java2s;

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static String[] getTokenArray(String sequence, String[] chain, boolean paired, boolean includeDelim)
            throws IllegalArgumentException {
        return (String[]) tokenize(sequence, chain, paired, includeDelim).toArray(new String[0]);
    }//  ww  w.  jav a2  s .c o m

    /**
     * Moves through the chain of delimiters in order until one of the tokens is
     * not found in the remainder of the string.
     * 
     * @param sequence
     *            to be tokenized.
     * @param chain
     *            list of delimiters to use cyclically.
     * @param paired
     *            process delimiters in pairs, returning token between them
     * @param includeDelim
     *            append delimiter to token
     * @return List of token Strings.
     * @throws IllegalArgumentException
     *             if original String is null
     */
    public static List tokenize(String sequence, String[] chain, boolean paired, boolean includeDelim)
            throws IllegalArgumentException {
        List tokens = new ArrayList();
        if (sequence == null)
            return null;
        if (chain == null)
            throw new IllegalArgumentException("no delimiter chain defined");
        if (chain.length == 0) {
            tokens.add(sequence);
            if (paired && chain.length % 2 != 0)
                throw new IllegalArgumentException(
                        "paired tokenization requires even-number of delimiters in chain");
        } else {
            int[] markers = { 0, -1 };
            int i = 0;
            int len = chain.length;
            int incr = paired ? 2 : 1;
            while (true) {
                if (paired)
                    addToken(sequence, chain[i], chain[i + 1], markers, includeDelim, tokens);
                else
                    addToken(sequence, chain[i], markers, includeDelim, tokens);
                if (markers[0] >= sequence.length())
                    break;
                i = (i + incr) % len;
            }
        }
        return tokens;
    }

    private static void addToken(String sequence, String delim, int[] markers, boolean includeDelim, List tokens) {
        markers[1] = sequence.indexOf(delim, markers[0]);
        if (markers[1] < 0)
            markers[1] = sequence.length();
        else if (includeDelim)
            markers[1] += delim.length();
        tokens.add(sequence.substring(markers[0], markers[1]));
        if (includeDelim)
            markers[0] = markers[1];
        else
            markers[0] = markers[1] + delim.length();
    }

    private static void addToken(String sequence, String start, String end, int[] markers, boolean includeDelim,
            List tokens) {
        markers[1] = sequence.indexOf(start, markers[0]);
        if (markers[1] < 0)
            markers[1] = sequence.length();
        else {
            if (!includeDelim)
                markers[0] = markers[1] + start.length();
            else
                markers[0] = markers[1];
            markers[1] = sequence.indexOf(end, markers[0]);
            if (markers[1] < 0)
                markers[1] = sequence.length();
            else if (includeDelim)
                markers[1] = markers[1] + end.length();
        }
        tokens.add(sequence.substring(markers[0], markers[1]));
        if (includeDelim)
            markers[0] = markers[1];
        else
            markers[0] = markers[1] + end.length();
    }
}

Related

  1. getNameTokens(final String names)
  2. getParamFloat(StringTokenizer s)
  3. getParamString(StringTokenizer s)
  4. getSQLTokens(String query)
  5. getStringTokens(String str)
  6. getTokens(final String s)
  7. getTokens(String msg, String delim)
  8. getTokens(String str, String delim)
  9. getTokens(String str, String delimiter)