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, boolean returnDelims) 

Source Link

Document

Constructs a string tokenizer for the specified string.

Usage

From source file:DateParser.java

private static Calendar getCalendar(String isodate) {
    // YYYY-MM-DDThh:mm:ss.sTZD
    StringTokenizer st = new StringTokenizer(isodate, "-T:.+Z", true);

    Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    calendar.clear();//from w ww .  java 2s .  c o  m
    try {
        // Year
        if (st.hasMoreTokens()) {
            int year = Integer.parseInt(st.nextToken());
            calendar.set(Calendar.YEAR, year);
        } else {
            return calendar;
        }
        // Month
        if (check(st, "-") && (st.hasMoreTokens())) {
            int month = Integer.parseInt(st.nextToken()) - 1;
            calendar.set(Calendar.MONTH, month);
        } else {
            return calendar;
        }
        // Day
        if (check(st, "-") && (st.hasMoreTokens())) {
            int day = Integer.parseInt(st.nextToken());
            calendar.set(Calendar.DAY_OF_MONTH, day);
        } else {
            return calendar;
        }
        // Hour
        if (check(st, "T") && (st.hasMoreTokens())) {
            int hour = Integer.parseInt(st.nextToken());
            calendar.set(Calendar.HOUR_OF_DAY, hour);
        } else {
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            return calendar;
        }
        // Minutes
        if (check(st, ":") && (st.hasMoreTokens())) {
            int minutes = Integer.parseInt(st.nextToken());
            calendar.set(Calendar.MINUTE, minutes);
        } else {
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            return calendar;
        }

        //
        // Not mandatory now
        //

        // Secondes
        if (!st.hasMoreTokens()) {
            return calendar;
        }
        String tok = st.nextToken();
        if (tok.equals(":")) { // secondes
            if (st.hasMoreTokens()) {
                int secondes = Integer.parseInt(st.nextToken());
                calendar.set(Calendar.SECOND, secondes);
                if (!st.hasMoreTokens()) {
                    return calendar;
                }
                // frac sec
                tok = st.nextToken();
                if (tok.equals(".")) {
                    // bug fixed, thx to Martin Bottcher
                    String nt = st.nextToken();
                    while (nt.length() < 3) {
                        nt += "0";
                    }
                    nt = nt.substring(0, 3); // Cut trailing chars..
                    int millisec = Integer.parseInt(nt);
                    // int millisec = Integer.parseInt(st.nextToken()) * 10;
                    calendar.set(Calendar.MILLISECOND, millisec);
                    if (!st.hasMoreTokens()) {
                        return calendar;
                    }
                    tok = st.nextToken();
                } else {
                    calendar.set(Calendar.MILLISECOND, 0);
                }
            } else {
                throw new RuntimeException("No secondes specified");
            }
        } else {
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
        }
        // Timezone
        if (!tok.equals("Z")) { // UTC
            if (!(tok.equals("+") || tok.equals("-"))) {
                throw new RuntimeException("only Z, + or - allowed");
            }
            boolean plus = tok.equals("+");
            if (!st.hasMoreTokens()) {
                throw new RuntimeException("Missing hour field");
            }
            int tzhour = Integer.parseInt(st.nextToken());
            int tzmin = 0;
            if (check(st, ":") && (st.hasMoreTokens())) {
                tzmin = Integer.parseInt(st.nextToken());
            } else {
                throw new RuntimeException("Missing minute field");
            }
            if (plus) {
                calendar.add(Calendar.HOUR, -tzhour);
                calendar.add(Calendar.MINUTE, -tzmin);
            } else {
                calendar.add(Calendar.HOUR, tzhour);
                calendar.add(Calendar.MINUTE, tzmin);
            }
        }
    } catch (NumberFormatException ex) {
        throw new RuntimeException("[" + ex.getMessage() + "] is not an integer");
    }
    return calendar;
}

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  .ja va 2s  .  c o 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:Main.java

protected static String replaceNewLineWithLineBreak(String source) {
    String str = null;/*from  ww  w  . j  a  va 2  s .  c  om*/

    if (source != null) {
        StringBuffer sbuffer = new StringBuffer();
        StringTokenizer tkzer = new StringTokenizer(source, "\n", true);
        String token = null;
        while (tkzer.hasMoreTokens()) {
            token = tkzer.nextToken();
            if ("\n".equals(token)) {
                sbuffer.append("<text:line-break/>");
            } else {
                sbuffer.append(token);
            }
        }

        str = sbuffer.toString();
    }

    return str;
}

From source file:RequestUtil.java

/**
 * Delete a cookie except the designated variable name from CookieString
 * //w ww  .  java  2 s .  co  m
 * @param cookieString
 * @param paramName
 * @return
 */
public static String removeCookieParam(String cookieString, Set<String> paramNames) {
    StringTokenizer tok = new StringTokenizer(cookieString, ";", false);
    String resultCookieString = "";

    while (tok.hasMoreTokens()) {
        String token = tok.nextToken();
        int i = token.indexOf("=");
        if (i > -1) {
            for (String paramName : paramNames) {
                String name = token.substring(0, i).trim();
                if (paramName.equalsIgnoreCase(name)) {
                    if (resultCookieString.length() > 0)
                        resultCookieString += ";";
                    resultCookieString += token;
                }
            }
        } else {
            // we have a bad cookie.... just let it go
        }
        if (paramNames.isEmpty()) {
            if (resultCookieString.length() > 0)
                resultCookieString += ";";
            resultCookieString += token;
        }
    }
    return resultCookieString.trim();
}

From source file:com.bailen.radioOnline.recursos.Jamendo.java

public Cancion[] canciones(Vector<Integer> ids) {
    String url = "http://api.jamendo.com/v3.0/tracks/?client_id=567e43c5&format=jsonpretty&limit=10&imagesize=600&id=";
    for (Integer id : ids) {
        url += id + "+";
    }/*w  ww. ja v  a2 s  .  co m*/

    String canc = new RestTemplate().getForObject(url, String.class, url);
    StringTokenizer st = new StringTokenizer(canc, "[", true);
    st.nextToken();
    st.nextToken();
    canc = "[" + st.nextToken();
    try {

        ObjectMapper a = new ObjectMapper();
        Cancion[] listilla = a.readValue(canc, Cancion[].class);
        return listilla;

    } catch (Exception e) {
        return null;
    }

}

From source file:Main.java

/**
 * Find the xpath element under the given root. As the xpath requirement is very
 * simple, so we avoid using XPath API//from  www  . j  a  v a2  s .  co m
 * @param root - the root element that search begins
 * @param xpath - the path from the root
 * @return - the xpath defined element under the given root.
 */
public static Element findXPathElement(Element root, String xpath) {
    if (root == null)
        return null;
    if (xpath == null || xpath.trim().equals(""))
        return root;
    xpath = toRelativePath(xpath, root.getNodeName());
    StringTokenizer st = new StringTokenizer(xpath, "/", false);
    String sitem;
    Element item = root;
    NodeList list;

    boolean first = true;
    while (st.hasMoreTokens()) {
        sitem = st.nextToken();
        if (first && sitem.equals(item.getNodeName())) {
            first = false;
        } else {
            list = item.getElementsByTagName(sitem);
            if (list.getLength() < 1)
                return null;
            item = (Element) (list.item(0));
        }
    }

    return item;
}

From source file:marytts.util.string.StringUtils.java

public static String deblank(String str) {
    StringTokenizer s = new StringTokenizer(str, " ", false);
    StringBuilder strRet = new StringBuilder();

    while (s.hasMoreElements())
        strRet.append(s.nextElement());//from  ww  w  .j a v  a 2  s  .c om

    return strRet.toString();
}

From source file:cc.recommenders.utils.parser.MavenVersionParser.java

@Override
public Version parse(final String version) throws IllegalArgumentException {
    int major = 0;
    int minor = 0;
    int micro = 0;
    String qualifier = "";
    try {//from  w  w w .  ja  v a  2s . c  om
        final StringTokenizer tokenizer = new StringTokenizer(version, ".-", true);
        major = parseInt(tokenizer);
        if (tokenizer.hasMoreTokens()) {
            consumeDelimiter(tokenizer, ".");
            minor = parseInt(tokenizer);
            if (tokenizer.hasMoreTokens()) {
                consumeDelimiter(tokenizer, ".");
                micro = parseInt(tokenizer);
                if (tokenizer.hasMoreTokens()) {
                    consumeDelimiter(tokenizer, "-", ".");
                    qualifier = parseString(tokenizer);
                    // everything that follows after the third separator is treated as being part of the qualifier
                    while (tokenizer.hasMoreElements()) {
                        qualifier += tokenizer.nextToken();
                    }
                }
            }
        }
    } catch (final NoSuchElementException e) {
        Throws.throwIllegalArgumentException("couldn't convert string into version: '%s'", version);
    }
    return Version.create(major, minor, micro, qualifier);
}

From source file:Main.java

private static List<String> splitTokens(String sentenceTosearch) {
    StringTokenizer tokenizer = new StringTokenizer(sentenceTosearch, "'", true);
    List<String> tokens = new ArrayList<>();
    while (tokenizer.hasMoreElements()) {
        tokens.add(tokenizer.nextToken());
    }/*  w  w  w  .j a  v  a2  s . c  om*/
    return tokens;
}

From source file:com.addthis.hydra.data.query.BoundedValue.java

public BoundedValue parse(String tok, MutableInt nextColumn) {
    StringTokenizer st = new StringTokenizer(LessBytes.urldecode(tok), "<>=", true);
    name = st.nextToken();/*from  w w  w.  j  a  va  2  s  .  c om*/
    while (st.hasMoreTokens()) {
        String next = st.nextToken();
        if (next.equals(">") && st.hasMoreTokens()) {
            gt = Long.parseLong(st.nextToken());
            bounded = true;
        }
        if (next.equals("<") && st.hasMoreTokens()) {
            lt = Long.parseLong(st.nextToken());
            bounded = true;
        }
        if (next.equals("=") && st.hasMoreTokens()) {
            eq = Long.parseLong(st.nextToken());
            bounded = true;
        }
    }
    return this;
}