Example usage for java.util StringTokenizer nextToken

List of usage examples for java.util StringTokenizer nextToken

Introduction

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

Prototype

public String nextToken() 

Source Link

Document

Returns the next token from this string tokenizer.

Usage

From source file:gov.nasa.ensemble.common.ERGBUtils.java

/**
 * COPIED FROM org.eclipse.jface.resource.StringConverter
 * /*from  w  w  w  .j  a  va  2 s.  c om*/
 * Converts the given value into an SWT RGB color value.
 * This method fails if the value does not represent an RGB
 * color value.
 * <p>
 * A valid RGB color value representation is a string of the form
 * <code><it>red</it>,<it>green</it></code>,<it>blue</it></code> where
 * <code><it>red</it></code>, <it>green</it></code>, and 
 * <code><it>blue</it></code> are valid ints.
 * </p>
 *
 * @param value the value to be converted
 * @return the value as an RGB color value
 * @exception DataFormatException if the given value does not represent
 *   an RGB color value
 */
private static ERGB asRGB(String value) throws DataFormatException {
    if (value == null) {
        throw new DataFormatException("Null doesn't represent a valid RGB"); //$NON-NLS-1$
    }
    StringTokenizer stok = new StringTokenizer(value, ","); //$NON-NLS-1$

    try {
        String red = stok.nextToken().trim();
        String green = stok.nextToken().trim();
        String blue = stok.nextToken().trim();
        int rval = 0, gval = 0, bval = 0;
        try {
            rval = Integer.parseInt(red);
            gval = Integer.parseInt(green);
            bval = Integer.parseInt(blue);
        } catch (NumberFormatException e) {
            throw new DataFormatException(e.getMessage());
        }
        return new ERGB(rval, gval, bval);
    } catch (NoSuchElementException e) {
        throw new DataFormatException(e.getMessage());
    }
}

From source file:Main.java

public static String[] split(final String string, final String tag) {
    StringTokenizer str = new StringTokenizer(string, tag);
    String[] result = new String[str.countTokens()];
    int index = 0;
    for (; str.hasMoreTokens();) {
        result[index++] = str.nextToken();
    }/*from  ww  w.  jav  a  2 s .  c  o m*/
    return result;
}

From source file:Main.java

public static Locale getLocale(String locale) {
    if (locale == null)
        return Locale.getDefault();

    StringTokenizer localeTokenizer = new StringTokenizer(locale, "_");
    if (localeTokenizer.countTokens() == 1) {
        return new Locale(locale);
    } else {// w  w  w . j  a  v a 2s .c  o m
        return new Locale(localeTokenizer.nextToken(), localeTokenizer.nextToken());
    }
}

From source file:org.mycontroller.standalone.api.jaxrs.utils.RestUtils.java

public static User getUser(HttpRequest request) {
    //Get request headers
    final HttpHeaders headers = request.getHttpHeaders();

    //Fetch authorization header
    final List<String> authorization = headers.getRequestHeader(AUTHORIZATION_PROPERTY);

    //If no authorization information present; nothing to do
    if (authorization == null || authorization.isEmpty()) {
        return null;
    }//w ww  . j a  v a  2  s  .  c  o m

    //Get encoded username and password
    final String encodedUserPassword = authorization.get(0).replaceFirst(AUTHENTICATION_SCHEME + " ", "");

    //Decode username and password
    String usernameAndPassword;
    try {
        usernameAndPassword = new String(Base64.decode(encodedUserPassword));
    } catch (IOException e) {
        return null;
    }

    //Split username and password tokens
    final StringTokenizer tokenizer = new StringTokenizer(usernameAndPassword, ":");
    final String username = tokenizer.nextToken();
    //final String password = tokenizer.nextToken();
    return UserMapper.getUser(username);
}

From source file:Main.java

/**
 * gets the character encoding from the String representation of an XML file.
 * @param sXml//from  w  w  w  .jav  a  2s. com
 * @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

public static Color convertHexStringToColor(String str) throws NumberFormatException {
    int multiplier = 1;
    StringTokenizer tokenizer = new StringTokenizer(str, " \t\r\n\b:;[]()+");
    while (tokenizer.hasMoreTokens()) {
        multiplier = 1;/*from  ww  w .  j  a  v a 2 s .c  o m*/
        String token = tokenizer.nextToken();
        if (null == token) {
            throw new NumberFormatException(str);
        }
        if (token.startsWith("-")) {
            multiplier = -1;
            token = token.substring(1);
        }
        int point_index = token.indexOf(".");
        if (point_index > 0) {
            token = token.substring(0, point_index);
        } else if (point_index == 0) {
            return new Color(0);
        }
        try {
            if (token.startsWith("0x")) {
                return new Color(multiplier * Integer.parseInt(token.substring(2), 16));
            } else if (token.startsWith("#")) {
                return new Color(multiplier * Integer.parseInt(token.substring(1), 16));
            } else if (token.startsWith("0") && !token.equals("0")) {
                return new Color(multiplier * Integer.parseInt(token.substring(1), 8));
            } else {
                return new Color(multiplier * Integer.parseInt(token));
            }
        } catch (NumberFormatException e) {
            continue;
        }
    }
    throw new NumberFormatException(str);

}

From source file:cooccurrence.emf.java

/**
 * Outer Method to read the file content and populate it in the cooccur
 * hashmap/*from w ww . j  a v  a2 s . c  o m*/
 *
 * @param filePath
 * @param cooccur
 * @return
 */
private static long readFileContents(String filePath, HashMap<String, HashMap<String, Double>> cooccur) {
    long totalCount = 0;
    try {

        BufferedReader br = new BufferedReader(new FileReader(filePath));
        String line = "";
        while ((line = br.readLine()) != null) {
            StringTokenizer tok = new StringTokenizer(line, "\t");
            if (tok.countTokens() == 3) {
                String from = tok.nextToken();
                String to = tok.nextToken();
                Double count = Double.parseDouble(tok.nextToken());
                totalCount += count;
                addToMatrix(from, to, count, cooccur);

            }
        }

    } catch (FileNotFoundException ex) {
        Logger.getLogger(pmi.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(pmi.class.getName()).log(Level.SEVERE, null, ex);
    }
    return totalCount;
}

From source file:Main.java

public static ArrayList<String> splitInputStringIntoWords(String msg) {

    StringTokenizer token = new StringTokenizer(msg, " ");

    ArrayList<String> wordsList = new ArrayList<String>();

    String nextWord;// www  .j a v a 2  s. c  o m
    while (token.hasMoreTokens()) {

        nextWord = token.nextToken();
        // Log.d(TAG, " inside getWords while loop.. nextToken - " +
        // nextWord);

        wordsList.add(nextWord);

    }

    return wordsList;
}

From source file:Main.java

/**
 * Convert a full XML name to its generic name. This is done by checking if
 * each part of the name is complex (if it consists of a generic name plus a
 * specific name). If so, then the specific part is removed. Example of a
 * complex name : model.infrastructure.node-edge.spdata Result of
 * getGenericName : model.infrastructure.node.spdata
 *
 * @param name The XML name/* w  w  w  .ja  v  a 2  s .  co m*/
 * @param removeArrays Wether array elements have to be removed from the
 * name.
 * @return The result
 */
public static String getGenericName(String fullName, boolean removeArrays) {
    StringTokenizer s = new StringTokenizer(fullName, ".");
    String result = "", tmp;
    int dashIndex;
    while (s.hasMoreTokens()) {
        tmp = s.nextToken();
        if (removeArrays && (("array").equals(tmp) || ("element").equals(tmp))) {
            continue;
        }
        if (result.length() > 0) {
            result += ".";
        }
        if ((dashIndex = tmp.indexOf('-')) == -1) {
            result += tmp;
        } else {
            result += tmp.substring(0, dashIndex);
        }
    }
    return result;
}

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

/**
 * Parsers the query string of the URI into a map of key-value pairs
 *//*  ww w. j  a va2s  .  com*/
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;
}