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) 

Source Link

Document

Constructs a string tokenizer for the specified string.

Usage

From source file:com.github.walterfan.util.http.URLHelper.java

/**
 * Parsers the query string of the URI into a map of key-value pairs
 *//*from w w  w.j  a  v  a  2  s.  co  m*/
public static Map<String, String> parseQuery(String query) {
    Map<String, String> answer = new HashMap<String, String>();
    if (query != null) {
        StringTokenizer iter = new StringTokenizer(query, "&");
        while (iter.hasMoreTokens()) {
            String pair = iter.nextToken();
            addKeyValuePair(answer, pair);
        }
    }
    return answer;
}

From source file:Main.java

/**
 * Find an element using XPath-quotation expressions. Path must not including
 * the context element, path elements can be separated by / or .,
 * and namespace is NOT supported./*from  w  w  w  .  j  a v  a  2 s .  c o m*/
 * @param context Element to start the search from, cannot be null.
 * @param path XPath-quotation expression, cannot be null.
 * @return the first matched element if there are matches, otherwise
 * return null.
 */
public static Element getElementByPath(Element context, String path) {
    Element cur = context;
    StringTokenizer tokens = new StringTokenizer(path, "/");

    while (tokens.hasMoreTokens()) {
        String name = tokens.nextToken();

        cur = getChildElement(cur, name);
        if (cur == null) {
            return null;
        }
    }
    return cur;
}

From source file:gov.nih.nci.lv.util.LVUtils.java

/**
 * converts a ids seperated by a comma to a set.
 * @param ids ids/*from  www.jav a 2s. c o  m*/
 * @param delimiter delimiter
 * @return Set
 */
public static Set<Long> convertStringToSet(String ids, String delimiter) {
    Set<Long> labs = new HashSet<Long>();
    if (StringUtils.isEmpty(ids)) {
        return labs;
    }
    StringTokenizer st = new StringTokenizer(ids, delimiter);
    while (st.hasMoreTokens()) {
        labs.add(new Long(st.nextElement().toString()));
    }
    return labs;
}

From source file:IPAddress.java

/**
 * Check if the specified address is a valid numeric TCP/IP address and return as an integer value
 * /*  www.jav  a  2 s.c o m*/
 * @param ipaddr String
 * @return int
 */
public final static int parseNumericAddress(String ipaddr) {

    //   Check if the string is valid

    if (ipaddr == null || ipaddr.length() < 7 || ipaddr.length() > 15)
        return 0;

    //   Check the address string, should be n.n.n.n format

    StringTokenizer token = new StringTokenizer(ipaddr, ".");
    if (token.countTokens() != 4)
        return 0;

    int ipInt = 0;

    while (token.hasMoreTokens()) {

        //   Get the current token and convert to an integer value

        String ipNum = token.nextToken();

        try {

            //   Validate the current address part

            int ipVal = Integer.valueOf(ipNum).intValue();
            if (ipVal < 0 || ipVal > 255)
                return 0;

            //   Add to the integer address

            ipInt = (ipInt << 8) + ipVal;
        } catch (NumberFormatException ex) {
            return 0;
        }
    }

    //   Return the integer address

    return ipInt;
}

From source file:Main.java

/**
 * Remove leading a trailing whitespace characters from each line of input
 * @param input//w w w  . ja va 2 s . c o m
 * @return
 */
public static String trim(String input) {
    final String newlineDelimiters = "\n\r\f"; //$NON-NLS-1$

    StringBuilder ret = new StringBuilder();
    StringTokenizer st = new StringTokenizer(input, newlineDelimiters);
    while (st.hasMoreTokens()) {
        ret.append(st.nextToken().replaceAll("^\\s+", "").replaceAll("\\s+$", "")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
        ret.append("\n"); //$NON-NLS-1$
    }
    return ret.toString();
}

From source file:architecture.common.util.LocaleUtils.java

/**
 * Converts a locale string like "en", "en_US" or "en_US_win" to a Java
 * locale object. If the conversion fails, null is returned.
 *
 * @param localeCode/*w  w  w.java2 s.c om*/
 *            the locale code for a Java locale. See the
 *            {@link java.util.Locale} class for more details.
 * @return The Java Locale that matches the locale code, or <tt>null</tt>.
 */
public static Locale localeCodeToLocale(String localeCode) {
    Locale locale = null;
    if (localeCode != null) {
        String language = null;
        String country = null;
        String variant = null;
        StringTokenizer tokenizer = new StringTokenizer(localeCode, "_");
        if (tokenizer.hasMoreTokens()) {
            language = tokenizer.nextToken();
            if (tokenizer.hasMoreTokens()) {
                country = tokenizer.nextToken();
                if (tokenizer.hasMoreTokens()) {
                    variant = tokenizer.nextToken();
                }
            }
        }
        locale = new Locale(language, ((country != null) ? country : ""), ((variant != null) ? variant : ""));
    }
    return locale;
}

From source file:Main.java

/**
 * Given an {@link NNTPClient} instance, and an integer range of messages, return 
 * an array of {@link Article} instances.
 * @param client /* www  .  j a v  a 2s. co m*/
 * @param lowArticleNumber
 * @param highArticleNumber
 * @return Article[] An array of Article
 * @throws IOException
 */
public static Article[] getArticleInfo(NNTPClient client, int lowArticleNumber, int highArticleNumber)
        throws IOException {
    Reader reader = null;
    Article[] articles = null;
    reader = (DotTerminatedMessageReader) client.retrieveArticleInfo(lowArticleNumber, highArticleNumber);

    if (reader != null) {
        String theInfo = readerToString(reader);
        StringTokenizer st = new StringTokenizer(theInfo, "\n");

        // Extract the article information
        // Mandatory format (from NNTP RFC 2980) is :
        // Subject\tAuthor\tDate\tID\tReference(s)\tByte Count\tLine Count

        int count = st.countTokens();
        articles = new Article[count];
        int index = 0;

        while (st.hasMoreTokens()) {
            StringTokenizer stt = new StringTokenizer(st.nextToken(), "\t");
            Article article = new Article();
            article.setArticleNumber(Integer.parseInt(stt.nextToken()));
            article.setSubject(stt.nextToken());
            article.setFrom(stt.nextToken());
            article.setDate(stt.nextToken());
            article.setArticleId(stt.nextToken());
            article.addHeaderField("References", stt.nextToken());
            articles[index++] = article;
        }
    } else {
        return null;
    }

    return articles;
}

From source file:Main.java

/**
 * Splits a string around matches of the given delimiter character.
 *
 * Where applicable, this method can be used as a substitute for
 * <code>String.split(String regex)</code>, which is not available
 * on a JSR169/Java ME platform.// w  w  w  .  ja v  a  2  s.co  m
 *
 * @param str the string to be split
 * @param delim the delimiter
 * @throws NullPointerException if str is null
 */
static public String[] split(String str, char delim) {
    if (str == null) {
        throw new NullPointerException("str can't be null");
    }

    // Note the javadoc on StringTokenizer:
    //     StringTokenizer is a legacy class that is retained for
    //     compatibility reasons although its use is discouraged in
    //     new code.
    // In other words, if StringTokenizer is ever removed from the JDK,
    // we need to have a look at String.split() (or java.util.regex)
    // if it is supported on a JSR169/Java ME platform by then.
    StringTokenizer st = new StringTokenizer(str, String.valueOf(delim));
    int n = st.countTokens();
    String[] s = new String[n];
    for (int i = 0; i < n; i++) {
        s[i] = st.nextToken();
    }
    return s;
}

From source file:StringUtils.java

public static List<String> explode(String s, String sep) {
    StringTokenizer st = new StringTokenizer(s, sep);
    List<String> v = new ArrayList<String>();
    for (; st.hasMoreTokens();) {
        v.add(st.nextToken());/*from   ww  w . ja v a  2  s .  com*/
    }
    return v;
}

From source file:com.salesmanager.core.util.StringUtil.java

public static Map parseTokenLine(String line, String delimiter) {

    BidiMap returnMap = new TreeBidiMap();

    if (StringUtils.isBlank(line) || StringUtils.isBlank(delimiter)) {
        return returnMap;
    }/*from  w w  w . j  a v a  2  s .  com*/

    StringTokenizer st = new StringTokenizer(line, delimiter);

    int count = 0;
    while (st.hasMoreTokens()) {
        String value = st.nextToken();
        returnMap.put(value, count);
        count++;
    }

    return returnMap;

}