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:com.sshtools.common.util.X11Util.java

public static X11Cookie getCookie(XDisplay xdisplay) {
    if (System.getProperty("os.name").startsWith("Windows"))
        return getRNDCookie();
    log.debug("Getting X11 cookie using xauth");
    try {//from ww w  .j  ava  2  s . c om
        String host = xdisplay.getHost();
        String display = host + ":" + xdisplay.getDisplay();
        String cmd = "xauth list " + display + " 2>/dev/null";
        if (host == null || host.equals("localhost") || host.equals("unix")) {
            cmd = "xauth list :" + xdisplay.getDisplay() + " 2>/dev/null";
        }

        Process process = null;
        InputStream in = null;
        OutputStream out = null;
        try {
            log.debug("Executing " + cmd);
            process = Runtime.getRuntime().exec(cmd);
            BufferedReader reader = new BufferedReader(new InputStreamReader(in = process.getInputStream()));
            out = process.getOutputStream();
            String line = null;
            String cookie = null;
            while ((line = reader.readLine()) != null) {
                log.debug(line);
                StringTokenizer t = new StringTokenizer(line);
                try {
                    String fhost = t.nextToken();
                    String type = t.nextToken();
                    String value = t.nextToken();
                    if (cookie == null) {
                        cookie = value;
                        log.debug("Using cookie " + cookie);
                    }
                    return expand(type, cookie);
                } catch (Exception e) {
                    log.error("Unexpected response from xauth.", e);
                    log.warn("Trying random data.");
                    return getRNDCookie();
                }
            }
        } finally {
            IOUtil.closeStream(in);
            IOUtil.closeStream(out);
        }
        return getRNDCookie();
    } catch (Exception e) {
        log.warn("Had problem finding xauth data (" + e + ") trying random data.");
        return getRNDCookie();
    }
}

From source file:jnode.net.VMInetAddress.java

/**
 * Test if the hostname is an IP address and if so returns the address.
 * @param hostname/*w  w  w  .j av a  2  s. c o m*/
 * @return The ip address, or null if it is not an IP address.
 */
private static byte[] getHostnameAsAddress(String hostname) {
    final StringTokenizer tok = new StringTokenizer(hostname, ".");
    if (tok.countTokens() != 4) {
        return null;
    }
    try {
        final byte b1 = parseUnsignedByte(tok.nextToken());
        final byte b2 = parseUnsignedByte(tok.nextToken());
        final byte b3 = parseUnsignedByte(tok.nextToken());
        final byte b4 = parseUnsignedByte(tok.nextToken());
        return new byte[] { b1, b2, b3, b4 };
    } catch (NumberFormatException ex) {
        return null;
    }
}

From source file:Main.java

/**
 * parses comma or space separated list. A null return value means a wildcard.
 * @return List of tokens or null if the commaSeparatedListText is null, '*', or empty
 *///from   w  w w.  j  a v a 2  s .c o m
public static List<String> parseCommaSeparatedList(String commaSeparatedListText) {
    List<String> entries = null;
    if (commaSeparatedListText != null) {
        if (!"*".equals(commaSeparatedListText)) {
            StringTokenizer tokenizer = new StringTokenizer(commaSeparatedListText, ", ");
            while (tokenizer.hasMoreTokens()) {
                if (entries == null) {
                    entries = new ArrayList<String>();
                }
                entries.add(tokenizer.nextToken());
            }
        }
    }
    return entries;
}

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 .ja v  a 2  s  .  c  o 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:com.thruzero.common.core.infonode.builder.utils.SampleNodeBuilderUtils.java

public static String normalize(String elements) {
    StringBuilder result = new StringBuilder();
    elements = StringUtils.replace(elements, "\r", "");
    elements = StringUtils.replace(elements, "\n", "");
    StringTokenizer st = new StringTokenizer(elements, " ");

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

        result.append(StringUtils.trimToEmpty(token));
    }/*  w w w. j  a v a  2 s .  c o m*/

    return result.toString();
}

From source file:Main.java

public static int[] toIntArray(String s) {
    StringTokenizer st = new StringTokenizer(s);
    int[] array = new int[st.countTokens()];
    for (int i = 0; st.hasMoreTokens(); i++)
        array[i] = Integer.parseInt(st.nextToken());
    return array;
}

From source file:Main.java

/**
 * Replace all occurrences of the characters &, ', ", < and > by the escaped
 * characters &amp;, &quot;, &apos;, &lt; and &gt;
 *///from   w w  w. j a  va  2s .  c  om
public static String XMLEscape(String s) {
    if (s == null) {
        return "";
    }

    boolean contains = false;
    for (int i = 0; i < XML_ESCAPE_DELIMITERS.length(); i++) {
        if (s.indexOf(XML_ESCAPE_DELIMITERS.charAt(i)) != -1) {
            contains = true;
        }
    }

    if (!contains) {
        return s;
    }

    if (s.length() == 0) {
        return s;
    }

    StringTokenizer tokenizer = new StringTokenizer(s, XML_ESCAPE_DELIMITERS, true);
    StringBuffer result = new StringBuffer();

    while (tokenizer.hasMoreElements()) {
        String substring = tokenizer.nextToken();

        if (substring.length() == 1) {
            switch (substring.charAt(0)) {

            case '&':
                result.append("&amp;");
                break;

            //case '\'' :
            //    result.append("&apos;");
            //    break;

            case ';':
                result.append("\\;");
                break;

            case '<':
                result.append("&lt;");
                break;

            case '>':
                result.append("&gt;");
                break;

            case '\"':
                result.append("&quot;");
                break;

            //                case '\n' :
            //                    result.append("\\n");
            //                    break;

            default:
                result.append(substring);
            }
        } else {
            result.append(substring);
        }
    }

    return result.toString();
}

From source file:com.adaptris.mail.Pop3sReceiverFactory.java

private static String[] asArray(String s) {
    StringTokenizer st = new StringTokenizer(s, ",");
    List<String> l = new ArrayList<String>();
    while (st.hasMoreTokens()) {
        String tok = st.nextToken().trim();
        if (!isEmpty(tok))
            l.add(tok);/*from   w  w  w .  j a  v a  2  s .c o m*/
    }
    return l.toArray(new String[0]);
}

From source file:it.unipd.dei.ims.falcon.CmdLine.java

private static int[] parseIntArray(String s) {
    StringTokenizer t = new StringTokenizer(s, ",");
    int[] ia = new int[t.countTokens()];
    int ti = 0;/*from   w w  w  .j a  va2 s. co  m*/
    while (t.hasMoreTokens())
        ia[ti] = Integer.parseInt(t.nextToken());
    return ia;
}

From source file:com.bluexml.side.Framework.alfresco.shareLanguagePicker.CustomDispatcherServlet.java

public static void setLanguageFromLayoutParam(HttpServletRequest req) {
    String urlLang = req.getParameter("shareLang");
    HttpSession currentSession = req.getSession();
    String sessionLang = (String) currentSession.getAttribute("shareLang");

    //1st option: Select the url param shareLang
    if (urlLang != null) {
        I18NUtil.setLocale(I18NUtil.parseLocale(urlLang));
        currentSession.setAttribute("shareLang", urlLang);
    } else if (sessionLang != null) {
        I18NUtil.setLocale(I18NUtil.parseLocale(sessionLang));
    } else {/*from   w  ww.  j a  va2 s . co  m*/
        // set language locale from browser header
        String acceptLang = req.getHeader("Accept-Language");
        if (acceptLang != null && acceptLang.length() != 0) {
            StringTokenizer t = new StringTokenizer(acceptLang, ",; ");
            // get language and convert to java locale format
            String language = t.nextToken().replace('-', '_');
            I18NUtil.setLocale(I18NUtil.parseLocale(language));
        }
    }
}