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:FileUtils.java

private static ArrayList<String> splitPath(String path) {
    ArrayList<String> pathElements = new ArrayList<String>();
    for (StringTokenizer st = new StringTokenizer(path, File.separator); st.hasMoreTokens();) {
        String token = st.nextToken();
        if (token.equals(".")) {
            // do nothing
        } else if (token.equals("..")) {
            if (!pathElements.isEmpty()) {
                pathElements.remove(pathElements.size() - 1);
            }/*from w w  w  .  ja v  a2s.  c  o m*/
        } else {
            pathElements.add(token);
        }
    }
    return pathElements;
}

From source file:Main.java

/**
 * Normalize a uri containing ../ and ./ paths.
 *
 * @param uri The uri path to normalize/*from   ww w.  j  av a2 s.  c o  m*/
 * @return The normalized uri
 */
public static String normalize(String uri) {
    if ("".equals(uri)) {
        return uri;
    }
    int leadingSlashes;
    for (leadingSlashes = 0; leadingSlashes < uri.length()
            && uri.charAt(leadingSlashes) == '/'; ++leadingSlashes) {
    }
    boolean isDir = (uri.charAt(uri.length() - 1) == '/');
    StringTokenizer st = new StringTokenizer(uri, "/");
    LinkedList clean = new LinkedList();
    while (st.hasMoreTokens()) {
        String token = st.nextToken();
        if ("..".equals(token)) {
            if (!clean.isEmpty() && !"..".equals(clean.getLast())) {
                clean.removeLast();
                if (!st.hasMoreTokens()) {
                    isDir = true;
                }
            } else {
                clean.add("..");
            }
        } else if (!".".equals(token) && !"".equals(token)) {
            clean.add(token);
        }
    }
    StringBuffer sb = new StringBuffer();
    while (leadingSlashes-- > 0) {
        sb.append('/');
    }
    for (Iterator it = clean.iterator(); it.hasNext();) {
        sb.append(it.next());
        if (it.hasNext()) {
            sb.append('/');
        }
    }
    if (isDir && sb.length() > 0 && sb.charAt(sb.length() - 1) != '/') {
        sb.append('/');
    }
    return sb.toString();
}

From source file:com.groupon.odo.proxylib.Utils.java

/**
 * Split string of comma-delimited ints into an a int array
 *
 * @param str/* ww  w  .  j  a  va  2s  . c  o  m*/
 * @return
 * @throws IllegalArgumentException
 */
public static int[] arrayFromStringOfIntegers(String str) throws IllegalArgumentException {
    StringTokenizer tokenizer = new StringTokenizer(str, ",");
    int n = tokenizer.countTokens();
    int[] list = new int[n];
    for (int i = 0; i < n; i++) {
        String token = tokenizer.nextToken();
        list[i] = Integer.parseInt(token);
    }
    return list;
}

From source file:net.sf.j2ep.model.AllowedMethodHandler.java

/**
 * Will go through all the methods sent in
 * checking to see that the method is allowed.
 * If it's allowed it will be included//from  w  w w.  j a va2s.  c om
 * in the returned value.
 * 
 * @param allowSent The header returned by the server
 * @return The allowed headers for this request
 */
public static String processAllowHeader(String allowSent) {
    StringBuffer allowToSend = new StringBuffer("");
    StringTokenizer tokenizer = new StringTokenizer(allowSent, ",");
    while (tokenizer.hasMoreTokens()) {
        String token = tokenizer.nextToken().trim().toUpperCase();
        if (allowedMethods.contains(token)) {
            allowToSend.append(token).append(",");
        }
    }

    return allowToSend.toString();
}

From source file:com.enonic.cms.core.preference.PreferenceScopeKey.java

public PreferenceScopeKey(String key) {

    if (key == null) {
        throw new IllegalArgumentException("Given key cannot be null");
    }/*from   w w w  . j a va2 s .  c o m*/

    if (key.indexOf(":") > -1) {
        StringTokenizer st = new StringTokenizer(key, ":");

        try {
            firstKey = Integer.valueOf(st.nextToken());
        } catch (NumberFormatException e) {
            throw new InvalidKeyException(key, this.getClass(), "first key not a number");
        }

        try {
            secondKey = Integer.valueOf(StringUtils.substringBefore(st.nextToken(), "."));
        } catch (NumberFormatException e) {
            throw new InvalidKeyException(key, this.getClass(), "second key not a number");
        }

        keyAsString = firstKey + ":" + secondKey;
    } else {
        try {
            firstKey = Integer.valueOf(key);
        } catch (NumberFormatException e) {
            throw new InvalidKeyException(key, this.getClass(), "first key not a number");
        }

        keyAsString = firstKey.toString();
    }

}

From source file:architecture.ee.web.attachment.AbstractAttachmentManager.java

protected static List<String> stringToList(String string) {
    List<String> list = new ArrayList<String>();
    if (string != null) {
        for (StringTokenizer tokens = new StringTokenizer(string, ","); tokens.hasMoreTokens(); list
                .add(tokens.nextToken()))
            ;// ww  w .j av a 2  s.c  om
    }
    return list;
}

From source file:arena.mail.MailAddressUtils.java

/**
 * Builds a list of internet address objects by parsing the
 * address list of the form "name <email>, name <email>"
 *//*from   w w w . j a v  a  2s. co m*/
public static InternetAddress[] parseAddressList(String addressList, String delim, String encoding) {
    if ((addressList == null) || (addressList.trim().length() == 0)) {
        return new InternetAddress[0];
    }
    Log log = LogFactory.getLog(MailAddressUtils.class);
    log.debug("Address list for parsing: " + addressList);
    StringTokenizer st = new StringTokenizer(addressList.trim(), delim);
    List<InternetAddress> addresses = new ArrayList<InternetAddress>();

    for (int n = 0; st.hasMoreTokens(); n++) {
        String fullAddress = st.nextToken().trim();
        if (fullAddress.equals("")) {
            continue;
        }

        try {
            int openPos = fullAddress.indexOf('<');
            int closePos = fullAddress.indexOf('>');

            if (openPos == -1) {
                addresses.add(new InternetAddress(
                        (closePos == -1) ? fullAddress.trim() : fullAddress.substring(0, closePos).trim()));
            } else if (closePos == -1) {
                addresses.add(new InternetAddress(fullAddress.substring(openPos + 1).trim(),
                        fullAddress.substring(0, openPos).trim(), encoding));
            } else {
                addresses.add(new InternetAddress(fullAddress.substring(openPos + 1, closePos).trim(),
                        fullAddress.substring(0, openPos).trim(), encoding));
            }
        } catch (Throwable err) {
            throw new RuntimeException("Error parsing address: " + fullAddress, err);
        }
    }

    log.debug("Found mail addresses: " + addresses);

    return (InternetAddress[]) addresses.toArray(new InternetAddress[addresses.size()]);
}

From source file:SocketAddressEncoder.java

public static InetSocketAddress decode(String str) throws UnknownHostException {
    StringTokenizer st = new StringTokenizer(str, ",");
    if (st.countTokens() != 6) {
        throw new Exception("Illegal amount of tokens");
    }/*from   w  w  w .  ja v a 2s. co  m*/

    StringBuffer sb = new StringBuffer();
    try {
        sb.append(convertAndValidateNumber(st.nextToken()));
        sb.append('.');
        sb.append(convertAndValidateNumber(st.nextToken()));
        sb.append('.');
        sb.append(convertAndValidateNumber(st.nextToken()));
        sb.append('.');
        sb.append(convertAndValidateNumber(st.nextToken()));
    } catch (IllegalArgumentException e) {
        throw new Exception(e.getMessage());
    }

    InetAddress dataAddr = InetAddress.getByName(sb.toString());

    // get data server port
    int dataPort = 0;
    try {
        int hi = convertAndValidateNumber(st.nextToken());
        int lo = convertAndValidateNumber(st.nextToken());
        dataPort = (hi << 8) | lo;
    } catch (IllegalArgumentException ex) {
        throw new Exception("Invalid data port: " + str);
    }

    return new InetSocketAddress(dataAddr, dataPort);
}

From source file:Main.java

/**
 * Parses a string of given stop:line entries into a list
 *//*from w ww .j a v  a2  s.com*/
public static ArrayList<String> parseString(String list) {
    ArrayList<String> result = new ArrayList<String>();
    StringTokenizer tokenizer = new StringTokenizer(list, ",");
    while (tokenizer.hasMoreTokens()) {
        result.add(tokenizer.nextToken());
    }
    return result;
}

From source file:IPAddress.java

/**
 * Check if the specified address is a valid numeric TCP/IP address
 * //from   w  ww  .  ja  v  a2s.  co m
 * @param ipaddr String
 * @return boolean
 */
public final static boolean isNumericAddress(String ipaddr) {

    //   Check if the string is valid

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

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

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

    while (token.hasMoreTokens()) {

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

        String ipNum = token.nextToken();

        try {
            int ipVal = Integer.valueOf(ipNum).intValue();
            if (ipVal < 0 || ipVal > 255)
                return false;
        } catch (NumberFormatException ex) {
            return false;
        }
    }

    //   Looks like a valid IP address

    return true;
}