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:edu.illinois.cs.cogcomp.wikifier.utils.io.InFile.java

public static List<String> tokenize(String s, String delims) {
     if (s == null)
         return null;
     List<String> res = new ArrayList<String>();
     StringTokenizer st = new StringTokenizer(s, delims);
     while (st.hasMoreTokens())
         res.add(st.nextToken());
     return res;//from   w  w w .j a v  a  2 s  .  c  o  m
 }

From source file:Main.java

/**
 * Creates a list of strings from a string within tokens. Empty tokens will
 * be omitted: If a string within tokens is <code>"a,,b,,c"</code> and the
 * delimiter string is <code>","</code>, the returned list of strings
 * contains the tree elements <code>"a", "b", "c"</code>.
 *
 * @param string    String within tokens
 * @param delimiter Delimiter that separates the tokens. Every character
 *                  of the delimiter string is a separate delimiter. If
 *                  the string within tokens is <code>"I,like:ice"</code>
 *                  and the delimiter string is <code>",:"</code>, the
 *                  returned list of strings contains the three elements
 *                  <code>"I", "like", "ice"</code>.
 * @return          List of strings/*www  .  ja v  a2  s.  co  m*/
 */
public static List<String> stringTokenToList(String string, String delimiter) {
    if (string == null) {
        throw new NullPointerException("string == null");
    }

    if (delimiter == null) {
        throw new NullPointerException("delimiter == null");
    }

    StringTokenizer tokenizer = new StringTokenizer(string, delimiter);
    List<String> list = new ArrayList<>(tokenizer.countTokens());

    while (tokenizer.hasMoreTokens()) {
        list.add(tokenizer.nextToken());
    }

    return list;
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.util.AuthUtil.java

/**
 * Use the information from the request to see if it has the Basic authorization header. If it does
 * validate it.// w ww.ja v  a2 s .co m
 * @param request
 * @return true if the user is authorized and false if the user is not authorized.
 * @throws IOException
 */
public static boolean isAuthorized(HttpServletRequest request) throws IOException {
    String username = null;
    String password = null;
    boolean authorized = false;
    String authHeader = request.getHeader(KEY_AUTHORIZATION);
    if (authHeader != null) {
        StringTokenizer st = new StringTokenizer(authHeader);
        if (st.hasMoreTokens()) {
            String basic = st.nextToken();
            if (basic.equalsIgnoreCase(KEY_BASIC)) {
                try {
                    String credentials = new String(
                            org.apache.commons.codec.binary.Base64.decodeBase64(st.nextToken().getBytes()),
                            MMXServerConstants.UTF8_ENCODING);
                    LOGGER.debug("Auth header {} ", authHeader);
                    int p = credentials.indexOf(":");
                    if (p != -1) {
                        username = credentials.substring(0, p).trim();
                        password = credentials.substring(p + 1).trim();
                    } else {
                        LOGGER.warn("Invalid authentication token");
                    }
                } catch (UnsupportedEncodingException e) {
                    LOGGER.warn("Couldn't retrieve authentication", e);
                }
            }
        }
    } else {
        LOGGER.info("Request is missing the authorization header");
    }
    AuthToken token = null;
    if (username != null && password != null) {
        try {
            token = AuthFactory.authenticate(username, password);
        } catch (ConnectionException e) {
            LOGGER.error("isAuthorized : ", e);
        } catch (InternalUnauthenticatedException e) {
            LOGGER.error("isAuthorized : ", e);
        } catch (UnauthorizedException e) {
            LOGGER.error("isAuthorized : ", e);
        }
    }
    if (token != null) {
        AdminManager manager = AdminManager.getInstance();
        authorized = manager.isUserAdmin(username, false);
        if (!authorized) {
            LOGGER.info("User:{} is not an admin. Not granting access", username);
        }
    }
    return authorized;
}

From source file:edu.illinois.cs.cogcomp.wikifier.utils.io.InFile.java

public static List<String> tokenize(String s) {
     if (s == null)
         return null;
     List<String> res = new ArrayList<String>();
     StringTokenizer st = new StringTokenizer(s, " \n\t\r");
     while (st.hasMoreTokens())
         res.add(st.nextToken());
     return res;/*from w w  w  .j  av  a 2s .  co m*/
 }

From source file:Main.java

/**
 * utility method to get the last token in a "."-delimited package+classname string
 *
 * @return/*from  ww  w.j ava2s  . c  o m*/
 */
private static String getSimpleName(String in) {
    if (in == null || in.length() == 0) {
        return in;
    }
    String out = null;
    StringTokenizer tokenizer = new StringTokenizer(in, ".");
    if (tokenizer.countTokens() == 0)
        out = in;
    else {
        while (tokenizer.hasMoreTokens()) {
            out = tokenizer.nextToken();
        }
    }
    return out;
}

From source file:Main.java

public static String restoreWithEndnote(String text, String holderString, String replacement, String noteTag,
        String noteSplit) {//from  www. jav  a 2s  .c o m
    int start = text.lastIndexOf(noteTag);
    String note = text.substring(start);
    text = text.substring(0, start);
    if (note.length() == noteTag.length())
        return text;
    StringBuilder sb = new StringBuilder(text);
    StringTokenizer token = new StringTokenizer(note.substring(1), noteSplit);
    int[] index = new int[token.countTokens()];
    for (int i = index.length - 1; i >= 0; i--) {
        index[i] = Integer.parseInt(token.nextToken());
    }

    int h_length = holderString.length();
    for (int i = 0; i < index.length; i++) {
        sb.replace(index[i], index[i] + h_length, replacement);
    }
    return sb.toString();
}

From source file:com.spotify.scio.extra.transforms.ProcessUtil.java

static String[] tokenizeCommand(String command) {
    StringTokenizer st = new StringTokenizer(command);
    String[] cmdArray = new String[st.countTokens()];
    for (int i = 0; st.hasMoreTokens(); i++)
        cmdArray[i] = st.nextToken();
    return cmdArray;
}

From source file:Main.java

public static Pattern compilePattern(String key, boolean ignoreCase) {
    StringTokenizer stk = new StringTokenizer(key, "*?", true);
    StringBuilder regex = new StringBuilder();
    while (stk.hasMoreTokens()) {
        String tk = stk.nextToken();
        char ch1 = tk.charAt(0);
        if (ch1 == '*') {
            regex.append(".*");
        } else if (ch1 == '?') {
            regex.append(".");
        } else {//from w ww  . j  av  a2 s  .  co  m
            regex.append("\\Q").append(tk).append("\\E");
        }
    }
    return Pattern.compile(regex.toString(), ignoreCase ? Pattern.CASE_INSENSITIVE : 0);
}

From source file:Main.java

public static String decodePath(String path, String delimiter, String characterSet)
        throws UnsupportedEncodingException {
    final StringTokenizer tokenizer = new StringTokenizer(path, delimiter);
    final StringBuilder builder = new StringBuilder(path.length());

    while (tokenizer.hasMoreTokens()) {
        builder.append(URLDecoder.decode(tokenizer.nextToken(), characterSet));
        builder.append(delimiter);//  www .ja v  a 2  s .c o m
    }
    builder.delete(builder.length() - delimiter.length(), builder.length());
    return builder.toString();
}

From source file:Main.java

public static String encodePath(String path, String delimiter, String characterSet)
        throws UnsupportedEncodingException {
    final StringTokenizer tokenizer = new StringTokenizer(path, delimiter);
    final StringBuilder builder = new StringBuilder(path.length());

    while (tokenizer.hasMoreTokens()) {
        builder.append(URLEncoder.encode(tokenizer.nextToken(), characterSet));
        builder.append(delimiter);// www. j av  a2s  .  c  om
    }
    builder.delete(builder.length() - delimiter.length(), builder.length());
    return builder.toString();
}