Example usage for java.util StringTokenizer StringTokenizer

List of usage examples for java.util StringTokenizer StringTokenizer

Introduction

In this page you can find the example usage for java.util StringTokenizer StringTokenizer.

Prototype

public StringTokenizer(String str, String delim, boolean returnDelims) 

Source Link

Document

Constructs a string tokenizer for the specified string.

Usage

From source file:Main.java

public static long[] strToArrayOfLong(String str) {
    StringTokenizer tmpst = new StringTokenizer(str, "|", false);
    int len = tmpst.countTokens();
    long[] retl = new long[len];

    for (int i = 0; i < len; i++) {
        retl[i] = Long.parseLong(tmpst.nextToken());
    }//w  w w  .jav a  2 s  .c o m

    return retl;
}

From source file:Main.java

public static String[] strToArrayOfString(String str) {
    if (str == null) {
        return new String[0];
    }//from  ww  w .j a v a  2 s  .  c om
    StringTokenizer tmpst = new StringTokenizer(str, "|", false);
    int len = tmpst.countTokens();
    String[] retl = new String[len];

    for (int i = 0; i < len; i++) {
        retl[i] = tmpst.nextToken();
    }

    return retl;
}

From source file:Main.java

public static String xmlEncoding(String input, String delimiter) {
    if (input == null || input.equals(""))
        return input;

    delimiter += '&';

    StringTokenizer tmpst = new StringTokenizer(input, delimiter, true);
    StringBuffer tmpsb = new StringBuffer(input.length() + 100);
    String tmps = null;//w w  w.  j  a  v  a 2  s  .  co  m
    while (tmpst.hasMoreTokens()) {
        tmps = tmpst.nextToken();
        if (tmps.length() == 1 && delimiter.indexOf(tmps) >= 0) {
            switch (tmps.charAt(0)) {
            case '<':
                tmpsb.append("&lt;");
                break;
            case '>':
                tmpsb.append("&gt;");
                break;
            case '&':
                tmpsb.append("&amp;");
                break;
            case '\'':
                tmpsb.append("&apos;");
                break;
            case '\"':
                tmpsb.append("&quot;");
                break;
            case '\n':
                tmpsb.append("&#10;");
                break;
            case '\r':
                tmpsb.append("&#13;");
                break;
            case '\t':
                tmpsb.append("&#9;");
                break;
            }
        } else {
            tmpsb.append(tmps);
        }
    }

    return tmpsb.toString();
}

From source file:Main.java

public static String quote(String value) {
    if (value != null) {
        if (value.contains("'")) {
            if (value.contains("\"")) {
                // We've got something perverse like [["It's back!"]] (ie a mix of single and double quotes!)
                StringBuilder sb = new StringBuilder("concat(");

                StringTokenizer st = new StringTokenizer(value, "'\"", true);
                while (st.hasMoreTokens()) {
                    sb.append(quote(st.nextToken()));

                    if (st.hasMoreTokens()) {
                        sb.append(", ");
                    }// www . j  av  a 2s .co m
                }

                sb.append(")");

                return sb.toString();
            } else {
                return '"' + value + '"';
            }

        } else {
            return "'" + value + "'";
        }
    }

    return value;
}

From source file:Main.java

/**
 * Parses and returns CGI arguments./*from  ww  w . ja v a  2 s .co m*/
 * 
 * @param rest the string to parse
 * @return CGI arguments from <tt>rest</tt>
 */
public static Map<String, String> parseArgs(final String rest) {
    if (isEmpty(rest))
        return Collections.emptyMap();
    final Map<String, String> res = new HashMap<String, String>(3);
    for (StringTokenizer st = new StringTokenizer(rest, "&", false); st.hasMoreTokens();) {
        final String pair = st.nextToken();
        final int ieq = pair.indexOf('=');
        String key, val;
        if (ieq == -1) {
            key = pair;
            val = null;
        } else {
            key = pair.substring(0, ieq);
            val = pair.substring(ieq + 1);
        }
        res.put(key.trim(), val == null ? val : val.trim());
    }
    return res;
}

From source file:Main.java

public static void initializeIrregularVerbMap() {
    try {/*from w w  w  . ja  v a2  s .  co  m*/
        BufferedReader reader = new BufferedReader(new FileReader(new File(FILE_NAME_IRREGULAR_VERB)));
        String line = null;
        while ((line = reader.readLine()) != null) {
            StringTokenizer tokenizer = new StringTokenizer(line, "\t\n", false);
            String root = null;
            if (tokenizer.hasMoreTokens())
                root = tokenizer.nextToken().trim();

            while (tokenizer.hasMoreTokens()) {
                irregularVerbMap.put(tokenizer.nextToken().trim(), root);
            }
        }
    } catch (FileNotFoundException e) {
        System.out.println("File not found in the system.");
    } catch (IOException e1) {
        System.out.println("IOException");
    }
}

From source file:Main.java

public static void initializeMap(InputStream is, boolean isPositive) {
    try {//  w w w . j  a  v a  2  s .co m
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        String line = null;
        while ((line = reader.readLine()) != null) {
            StringTokenizer tokenizer = new StringTokenizer(line, "\t\n", false);
            String key = null;
            Vector<String> tokens = new Vector<String>();
            if (tokenizer.hasMoreTokens())
                key = tokenizer.nextToken();

            while (tokenizer.hasMoreTokens()) {
                tokens.add(tokenizer.nextToken());
            }
            if (key != null) {
                if (isPositive)
                    posMap.put(key, tokens);
                else
                    negMap.put(key, tokens);
            }
        }
    } catch (Exception e) {
    }
}

From source file:Main.java

/**
 * gets the character encoding from the String representation of an XML file.
 * @param sXml//from w  ww . ja v a 2 s  .  c  o  m
 * @return the encoding specified in the String, or "UTF-8" (default encoding for XML files) if none is specified.
 * @postcondition result != null
 */
public static String getXMLEncoding(String sXml) {
    final int iEncodingStart = sXml.indexOf("encoding=");
    String result = null;

    if (iEncodingStart > 0) {
        final String encbuf = sXml.substring(iEncodingStart);
        final StringTokenizer tokenizer = new StringTokenizer(encbuf, "\"'", true);
        boolean encfound = false;
        while (tokenizer.hasMoreTokens()) {
            sXml = tokenizer.nextToken();
            if (sXml.equals("'") || sXml.equals("\"")) {
                encfound = true;
            } else {
                if (encfound) {
                    result = sXml;
                    break;
                }
            }
        }
    }
    if (result == null) {
        result = "UTF-8";
    }
    assert result != null;
    return result;
}

From source file:Main.java

private static String[] split(String s, String delim, boolean includeDelim) {
    StringTokenizer tok = new StringTokenizer(s, delim, includeDelim);
    String[] parts = new String[tok.countTokens()];
    for (int i = 0; tok.hasMoreTokens(); i++) {
        parts[i] = tok.nextToken();/*from   w  w  w .  j a  v  a 2 s.co m*/
    }
    return parts;
}

From source file:Main.java

/**
 * Replace all occurrences of the characters &, ', ", < and > by the escaped
 * characters &amp;, &quot;, &apos;, &lt; and &gt;
 *//*from  www .  j a v  a2s . c o  m*/
public static String XMLEscape(String s) {
    if (s == null) {
        return "";
    }

    boolean contains = false;
    for (int i = 0; i < XML_ESCAPE_DELIMITERS.length(); i++) {
        if (s.indexOf(XML_ESCAPE_DELIMITERS.charAt(i)) != -1) {
            contains = true;
        }
    }

    if (!contains) {
        return s;
    }

    if (s.length() == 0) {
        return s;
    }

    StringTokenizer tokenizer = new StringTokenizer(s, XML_ESCAPE_DELIMITERS, true);
    StringBuffer result = new StringBuffer();

    while (tokenizer.hasMoreElements()) {
        String substring = tokenizer.nextToken();

        if (substring.length() == 1) {
            switch (substring.charAt(0)) {

            case '&':
                result.append("&amp;");
                break;

            //case '\'' :
            //    result.append("&apos;");
            //    break;

            case ';':
                result.append("\\;");
                break;

            case '<':
                result.append("&lt;");
                break;

            case '>':
                result.append("&gt;");
                break;

            case '\"':
                result.append("&quot;");
                break;

            //                case '\n' :
            //                    result.append("\\n");
            //                    break;

            default:
                result.append(substring);
            }
        } else {
            result.append(substring);
        }
    }

    return result.toString();
}