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) 

Source Link

Document

Constructs a string tokenizer for the specified string.

Usage

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./*from  w  ww  .j av a 2s .c  o 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:HelloResourceBundleExample.java

public Enumeration getKeys() {
  StringTokenizer keyTokenizer = new StringTokenizer(keys);

  return keyTokenizer;
}

From source file:TextLayoutLeft.java

public void paint(Graphics g) {
    d = getSize();/*from   ww  w .  j av a 2s . c o m*/
    g.setFont(f);
    if (fm == null) {
        fm = g.getFontMetrics();
        ascent = fm.getAscent();
        fh = ascent + fm.getDescent();
        space = fm.stringWidth(" ");
    }
    g.setColor(Color.black);
    StringTokenizer st = new StringTokenizer(
            "this is a text. this is a test <BR> this is a text. this is a test");
    int x = 0;
    int nextx;
    int y = 0;
    String word, sp;
    int wordCount = 0;
    String line = "";
    while (st.hasMoreTokens()) {
        word = st.nextToken();
        if (word.equals("<BR>")) {
            drawString(g, line, wordCount, fm.stringWidth(line), y + ascent);
            line = "";
            wordCount = 0;
            x = 0;
            y = y + (fh * 2);
        } else {
            int w = fm.stringWidth(word);
            if ((nextx = (x + space + w)) > d.width) {
                drawString(g, line, wordCount, fm.stringWidth(line), y + ascent);
                line = "";
                wordCount = 0;
                x = 0;
                y = y + fh;
            }
            if (x != 0) {
                sp = " ";
            } else {
                sp = "";
            }
            line = line + sp + word;
            x = x + space + w;
            wordCount++;
        }
    }
    drawString(g, line, wordCount, fm.stringWidth(line), y + ascent);
}

From source file:Main.java

public Enumeration getKeys() {
    StringTokenizer keyTokenizer = new StringTokenizer(keys);

    return keyTokenizer;
}

From source file:SimpleResourceBundleExample.java

public Enumeration getKeys() {
        StringTokenizer keyTokenizer = new StringTokenizer(keys);

        return keyTokenizer;
    }

From source file:com.conversantmedia.mapreduce.example.NamedOutputWordCountMapper.java

@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
    String line = value.toString();
    StringTokenizer tokenizer = new StringTokenizer(line);
    while (tokenizer.hasMoreTokens()) {
        word.set(tokenizer.nextToken().replaceAll("\\W", ""));
        context.write(word, ONE);/*  w w w. j av a 2  s .c om*/
    }

    // Debug output
    if (line.length() > 10) {
        multiOut.write("DEBUG", new Text(line.length() + ":"), value);
    }
}

From source file:NimbleTree.java

/**
 * A static constructor (is this the right term?) where a lisp-style s-expression
 * is passed in as a string. This can't be a true constructor because the result
 * is a tree over String, not a generic tree.
 *//* w w w  .  ja v a2 s.  co  m*/
public static NimbleTree<String> makeTreeOverStringFromSExpression(String input) {
    NimbleTree<String> tree = new NimbleTree<String>();
    Stack<TreeNode<String>> previousParents = new Stack<TreeNode<String>>();

    // Make sure the string is tokenizable
    // FIXME allow [] and maybe other delimiters?
    input = input.replace("(", " ( ");
    input = input.replace(")", " ) ");

    StringTokenizer st = new StringTokenizer(input);

    boolean firstTimeThrough = true;
    while (st.hasMoreTokens()) {
        String currTok = st.nextToken().trim();

        if (currTok.equals("")) {
            // Tokenizer gave us an empty token, do nothing.

        } else if (currTok.equals("(")) {
            // Get the *next* token and make a new subtree on it.
            currTok = st.nextToken().trim();

            if (firstTimeThrough == true) {
                // The tree is of the form "(x)"
                // This is the root node: just set its data
                firstTimeThrough = false;
                tree.getRoot().setData(currTok);

            } else {
                // This is the root of a new subtree. Save parent,
                // then set this node to be the new parent.
                tree.addChild(currTok);
                tree.getCurrentNode().getEnd().setID(tree.getNodeCount());
                previousParents.push(tree.getCurrentNode());
                tree.setCurrentNode(tree.getCurrentNode().getEnd());
            }

        } else if (currTok.equals(")")) {
            // Finished adding children to current parent. Go back
            // to previous parent (if there was none, it's because
            // current parent is root, so we're finished anyway).
            if (!previousParents.empty()) {
                tree.setCurrentNode(previousParents.pop());
            }

        } else {
            if (firstTimeThrough == true) {
                // The tree is of the form "x".
                // This is to be the root node: just set its data. 
                firstTimeThrough = false;
                tree.getRoot().setData(currTok);
            } else {
                // Add a child node to current parent.
                tree.addChild(currTok);
                tree.getCurrentNode().getEnd().setID(tree.getNodeCount());
            }
        }
    }

    return tree;
}

From source file:edu.umd.cfar.lamp.viper.geometry.Ellipse.java

/**
 * Constructs a new ellipse from the given string
 * @param S the string to parse - a space delimited list of four integers
 * @return the Ellipse represented by the string.
 * @throws BadAttributeDataException/*w w  w  .  j  a  v  a 2 s .c  o  m*/
 */
public static Ellipse valueOf(String S) {
    try {
        StringTokenizer st = new StringTokenizer(S);
        return new Ellipse(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()),
                Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()),
                Integer.parseInt(st.nextToken()));
    } catch (NoSuchElementException e1) {
        throw new BadAttributeDataException("Not enough integers for ellipse: " + S);
    } catch (NumberFormatException e2) {
        throw new BadAttributeDataException("Malformed ellipse string: " + S);
    }
}

From source file:Main.java

private static ArrayList<String> parseLine(String line) {
    StringTokenizer st = new StringTokenizer(line);
    ArrayList<String> tokens = new ArrayList<>();

    while (st.hasMoreTokens()) {
        tokens.add(st.nextToken());/*from   w  w w.  j av a  2  s.  c  o  m*/
    }

    return tokens;
}

From source file:com.conversantmedia.mapreduce.example.WordCountWithBlacklistMapper.java

@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
    String line = value.toString();
    StringTokenizer tokenizer = new StringTokenizer(line);
    while (tokenizer.hasMoreTokens()) {
        String nextWord = tokenizer.nextToken().replaceAll("\\W", "");
        if (!blacklistedWords.contains(nextWord)) {
            word.set(nextWord);//from   ww  w .j  a va2s  .  com
            context.write(word, ONE);
        }
    }
}