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: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");
    }//  w  w w  . ja v a2 s  .com

    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:marytts.features.FeatureRegistry.java

/**
 * Obtain a TargetFeatureComputer that knows how to compute features
 * for a Target using the given set of feature processor names. These names
 * must be known to the given Feature processor manager.
 * @param mgr /* w w  w . j  av  a  2s  .c  om*/
 * @param features a String containing the names of the
 * feature processors to use, separated by white space, and in the 
 * right order (byte-valued discrete feature processors first, then
 * short-valued, then continuous). If features is null, 
 * use all available features processors.
 * @return a target feature computer
 * @throws IllegalArgumentException if one of the features is not known to the manager
 */
public static TargetFeatureComputer getTargetFeatureComputer(FeatureProcessorManager mgr, String features) {
    if (features == null) {
        features = mgr.listFeatureProcessorNames();
    } else {
        // verify that each feature is known to the mgr
        StringTokenizer st = new StringTokenizer(features);
        while (st.hasMoreTokens()) {
            String feature = st.nextToken();
            if (mgr.getFeatureProcessor(feature) == null) {
                throw new IllegalArgumentException("Feature processor manager '" + mgr.getClass().toString()
                        + "' does not know the feature '" + feature + "'");
            }

        }
    }
    TargetFeatureComputer tfc = (TargetFeatureComputer) computers.get(mgr, features);
    if (tfc == null) {
        tfc = new TargetFeatureComputer(mgr, features);
    }
    return tfc;
}

From source file:com.sds.acube.ndisc.xnapi.XNApiUtils.java

/**
 *  ? ??   //w  ww. j  av a 2 s . com
 * 
 * @param rcvmsg
 *              ?
 * @param count
 *            ? 
 * @return ? ?  
 */
public static String[] getFileIds(String rcvmsg, int count) {
    String fileID[] = null;
    try {
        StringTokenizer sTK = new StringTokenizer(rcvmsg, XNApiConfig.DELIM_STR);
        if (XNApiConfig.ERROR.equals(sTK.nextToken())) {
            throw new NDiscException(sTK.nextToken().trim());
        }
        fileID = new String[count];
        for (int i = 0; i < count; i++) {
            fileID[i] = sTK.nextToken().trim();
        }
    } catch (Exception e) {
    }
    return fileID;
}

From source file:com.tacitknowledge.util.discovery.ClasspathUtils.java

/**
 * Returns the classpath as a list directory and archive names.
 *
 * @return the classpath as a list of directory and archive file names; if
 *         no components can be found then an empty list will be returned
 *//*from  w w w.  jav a 2 s .co  m*/
public static List getClasspathComponents() {
    List components = new LinkedList();

    // walk the classloader hierarchy, trying to get all the components we can
    ClassLoader cl = Thread.currentThread().getContextClassLoader();

    while ((null != cl) && (cl instanceof URLClassLoader)) {
        URLClassLoader ucl = (URLClassLoader) cl;
        components.addAll(getUrlClassLoaderClasspathComponents(ucl));

        try {
            cl = ucl.getParent();
        } catch (SecurityException se) {
            cl = null;
        }
    }

    // walking the hierarchy doesn't guarantee we get everything, so
    // lets grab the system classpath for good measure.
    String classpath = System.getProperty("java.class.path");
    String separator = System.getProperty("path.separator");
    StringTokenizer st = new StringTokenizer(classpath, separator);
    while (st.hasMoreTokens()) {
        String component = st.nextToken();
        // Calling File.getPath() cleans up the path so that it's using
        // the proper path separators for the host OS
        component = getCanonicalPath(component);
        components.add(component);
    }

    // Set removes any duplicates, return a list for the api.
    return new LinkedList(new HashSet(components));
}

From source file:Main.java

private static String convertStringToTime(String oldTime) {
    if (oldTime == null || oldTime.length() == 0) {
        return "00h00m00s";
    }//from ww  w.ja  va 2s. c  om

    boolean hasHours = (oldTime.indexOf('h') != -1);
    boolean hasMinutes = (oldTime.indexOf('m') != -1);
    StringBuffer rtn = new StringBuffer(oldTime);

    // Delete the seconds fields and replace other chars with colons
    rtn.deleteCharAt(oldTime.indexOf('s'));

    if (hasMinutes) {
        rtn.setCharAt(oldTime.indexOf('m'), ':');
    } else {
        rtn.insert(0, "00:");
    }

    if (hasHours) {
        rtn.setCharAt(oldTime.indexOf('h'), ':');
    } else {
        rtn.insert(0, "00:");
    }

    StringTokenizer st = new StringTokenizer(rtn.toString(), ":");
    int index = 1;
    String hh = "00";
    String mm = "00";
    String ss = "00";

    while (st.hasMoreTokens()) {
        String toke = st.nextToken();

        switch (index) {
        case 1:
            if (toke.length() < 2) {
                hh = "0" + toke;
            } else {
                hh = toke;
            }
            break;

        case 2:
            if (toke.length() < 2) {
                mm = "0" + toke;
            } else {
                mm = toke;
            }
            break;

        case 3:
            if (toke.length() < 2) {
                ss = "0" + toke;
            } else {
                ss = toke;
            }
            break;
        }

        index++;
    }

    return hh + "h" + mm + "m" + ss + "s";
}

From source file:com.zuoxiaolong.niubi.job.persistent.hibernate.HibernateNamingStrategy.java

private static String[] splitName(String someName) {
    StringTokenizer tokenizer = new StringTokenizer(someName, SEPARATOR);
    String[] tokens = new String[tokenizer.countTokens()];
    int i = 0;/*from   www  . j ava2 s .  co m*/
    while (tokenizer.hasMoreTokens()) {
        tokens[i] = tokenizer.nextToken();
        i++;
    }
    return tokens;
}

From source file:com.xpn.xwiki.render.macro.TableBuilder.java

public static Table build(String content) {
    Table table = new Table();
    StringTokenizer tokenizer = new StringTokenizer(content, "|\n", true);
    String lastToken = null;//from w w w. j  ava 2  s  .  co m
    boolean firstCell = true;
    while (tokenizer.hasMoreTokens()) {
        String token = tokenizer.nextToken();
        if (token.equals("\r")) {
            continue;
        }
        // If a token contains [, then all tokens up to one containing a ] are concatenated. Kind of a block marker.
        if (token.indexOf('[') != -1 && token.indexOf(']') == -1) {
            String linkToken = "";
            while (token.indexOf(']') == -1 && tokenizer.hasMoreTokens()) {
                linkToken += token;
                token = tokenizer.nextToken();
            }
            token = linkToken + token;
        }
        if ("\n".equals(token)) {
            // New line: either new row, or a literal newline.
            lastToken = (lastToken == null) ? "" : lastToken;
            if (!StringUtils.endsWith(lastToken, "\\")) {
                // A new row, not a literal newline.
                // If the last cell didn't contain any data, then it was skipped. Add a blank cell to compensate.
                if ((StringUtils.isEmpty(lastToken) || "|".equals(lastToken)) && !firstCell) {
                    table.addCell(" ");
                }
                table.newRow();
            } else {
                // A continued row, with a literal newline.
                String cell = lastToken;
                // Keep concatenating while the cell data ends with \\
                while (cell.endsWith("\\") && tokenizer.hasMoreTokens()) {
                    token = tokenizer.nextToken();
                    if (!"|".equals(token)) {
                        cell = cell + token;
                    } else {
                        break;
                    }
                }
                firstCell = false;
                table.addCell(cell.trim());
                if (!tokenizer.hasMoreTokens()) {
                    table.newRow();
                }
            }
        } else if (!"|".equals(token)) {
            // Cell data
            if (!token.endsWith("\\")) {
                // If the cell data ends with \\, then it will be continued. Current data is stored in lastToken.
                table.addCell(token.trim());
                firstCell = false;
            } else if (!tokenizer.hasMoreTokens()) {
                // Remove backslashes from the end
                while (token.endsWith("\\")) {
                    token = StringUtils.chop(token);
                }
                table.addCell(token.trim());
            }
        } else if ("|".equals(token)) {
            // Cell delimiter
            if ((StringUtils.isEmpty(lastToken) && firstCell) || "|".equals(lastToken)) {
                // If the last cell didn't contain any data, then it was skipped. Add a blank cell to compensate.
                table.addCell(" ");
                firstCell = false;
            } else if (StringUtils.endsWith(lastToken, "\\")) {
                // The last cell wasn't added because it ended with a continuation mark (\\). Add it now.
                table.addCell(lastToken.trim());
                firstCell = false;
            }
        }
        lastToken = token;
    }

    return table;
}

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

/**
 * Split string of comma-delimited ints into an a int array
 *
 * @param str//  w ww .j  a v  a  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:org.projecthdata.social.api.connect.HDataServiceProvider.java

private static StringBuilder getBaseUrl(String ehrUrl) {
    URI uri = URIBuilder.fromUri(ehrUrl).build();
    StringBuilder builder = new StringBuilder();
    builder.append(uri.getScheme()).append("://");
    builder.append(uri.getHost());//from  ww  w.j a  va2s  . co  m
    if (uri.getPort() >= 0) {
        builder.append(":").append(uri.getPort());
    }
    if (uri.getPath() != null) {
        StringTokenizer tokenizer = new StringTokenizer(uri.getPath(), "/");
        // if there is more than one path element, then the first one should
        // be the webapp name
        if (tokenizer.countTokens() > 1) {
            builder.append("/").append(tokenizer.nextToken());
        }
    }
    return builder;
}

From source file:com.lily.dap.web.util.WebUtils.java

/**
 *  If-None-Match Header,Etag.//from   ww  w .j  a v a2s .  c o  m
 * 
 * Etag,checkIfNoneMatchfalse, 304 not modify status.
 */
public static boolean checkIfNoneMatchEtag(HttpServletRequest request, HttpServletResponse response,
        String etag) {
    String headerValue = request.getHeader("If-None-Match");
    if (headerValue != null) {
        boolean conditionSatisfied = false;
        if (!headerValue.equals("*")) {
            StringTokenizer commaTokenizer = new StringTokenizer(headerValue, ",");

            while (!conditionSatisfied && commaTokenizer.hasMoreTokens()) {
                String currentToken = commaTokenizer.nextToken();
                if (currentToken.trim().equals(etag)) {
                    conditionSatisfied = true;
                }
            }
        } else {
            conditionSatisfied = true;
        }

        if (conditionSatisfied) {
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            response.setHeader("ETag", etag);
            return false;
        }
    }
    return true;
}