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

/**
 *
 * @param source/*from   ww  w. j  av a2 s. c  o  m*/
 *            ifconfig output
 * @param device
 *            device of interest
 * @return
 */
public static String[] findIPandNetmask(String source, String device) {
    int deviceIndex = source.indexOf(device);
    source = source.substring(deviceIndex);
    int nextLine = source.indexOf("\n");
    source = source.substring(nextLine);
    int startOfIP = source.indexOf(":");
    int endOfIP = source.indexOf(" ", startOfIP);
    String ip = source.substring(startOfIP + 1, endOfIP);

    int startOfMask = source.indexOf("Mask:");
    int endOfMask = source.indexOf("\n", startOfMask);
    String mask = source.substring(startOfMask + 5, endOfMask);

    StringTokenizer ipTokens = new StringTokenizer(ip, ".");
    StringTokenizer maskTokens = new StringTokenizer(mask, ".");

    int firstOctet = Integer.parseInt(ipTokens.nextToken()) & Integer.parseInt(maskTokens.nextToken());
    int secondOctet = Integer.parseInt(ipTokens.nextToken()) & Integer.parseInt(maskTokens.nextToken());
    int thirdOctet = Integer.parseInt(ipTokens.nextToken()) & Integer.parseInt(maskTokens.nextToken());
    int fourthOctet = Integer.parseInt(ipTokens.nextToken()) & Integer.parseInt(maskTokens.nextToken());

    String networkAddress = firstOctet + "." + secondOctet + "." + thirdOctet + "." + fourthOctet;

    return new String[] { networkAddress, mask };

}

From source file:de.mpg.imeji.presentation.util.LoginHelper.java

/**
 * Get handle of System administrator of eSciDoc instance.
 * //  w ww .  j  av a 2  s.  c  o m
 * @return
 */
//    public static String loginSystemAdmin()
//    {
//        String handle = null;
//        try
//        {
//            handle = login(PropertyReader.getProperty("framework.admin.username"),
//                    PropertyReader.getProperty("framework.admin.password"));
//        }
//        catch (Exception e)
//        {
//            sessionBean = (SessionBean)BeanHelper.getSessionBean(SessionBean.class);
//            BeanHelper
//                    .info(sessionBean.getLabel("error") + ", wrong administrator user. Check config file or FW: " + e);
//            logger.error("Error escidoc admin login", e);
//        }
//        return handle;
//    }

public static String login(String userName, String password) throws Exception {
    String frameworkUrl = PropertyReader.getProperty("escidoc.framework_access.framework.url");
    StringTokenizer tokens = new StringTokenizer(frameworkUrl, "//");
    tokens.nextToken();
    StringTokenizer hostPort = new StringTokenizer(tokens.nextToken(), ":");
    String host = hostPort.nextToken();
    int port = 80;
    if (hostPort.hasMoreTokens()) {
        port = Integer.parseInt(hostPort.nextToken());
    }
    HttpClient client = new HttpClient();
    client.getHttpConnectionManager().closeIdleConnections(1000);
    client.getHostConfiguration().setHost(host, port, "http");
    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    PostMethod login = new PostMethod(frameworkUrl + "/aa/j_spring_security_check");
    login.addParameter("j_username", userName);
    login.addParameter("j_password", password);
    try {
        client.executeMethod(login);
    } catch (Exception e) {
        throw new RuntimeException("Error login in " + frameworkUrl + "  status: " + login.getStatusCode()
                + " - " + login.getStatusText());
    }
    login.releaseConnection();
    CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
    Cookie[] logoncookies = cookiespec.match(host, port, "/", false, client.getState().getCookies());
    Cookie sessionCookie = logoncookies[0];
    PostMethod postMethod = new PostMethod("/aa/login");
    postMethod.addParameter("target", frameworkUrl);
    client.getState().addCookie(sessionCookie);
    client.executeMethod(postMethod);
    if (HttpServletResponse.SC_SEE_OTHER != postMethod.getStatusCode()) {
        throw new HttpException("Wrong status code: " + postMethod.getStatusCode());
    }
    String userHandle = null;
    Header headers[] = postMethod.getResponseHeaders();
    for (int i = 0; i < headers.length; ++i) {
        if ("Location".equals(headers[i].getName())) {
            String location = headers[i].getValue();
            int index = location.indexOf('=');
            userHandle = new String(Base64.decode(location.substring(index + 1, location.length())));
        }
    }
    if (userHandle == null) {
        throw new ServiceException("User not logged in.");
    }
    return userHandle;
}

From source file:Main.java

public static String[] strToArrayOfString(String str) {
    if (str == null) {
        return new String[0];
    }/*w ww.  java  2s .  c  o m*/
    StringTokenizer tmpst = new StringTokenizer(str, "|", false);
    int len = tmpst.countTokens();
    String[] retl = new String[len];

    for (int i = 0; i < len; i++) {
        retl[i] = tmpst.nextToken();
    }

    return retl;
}

From source file:Main.java

protected static String replaceNewLineWithLineBreak(String source) {
    String str = null;//from   w w  w. jav  a  2  s  .  c  o m

    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:net.sf.click.jquery.examples.util.StartupListener.java

private static String next(StringTokenizer tokenizer) {
    String token = tokenizer.nextToken().trim();
    if (token.startsWith("\"")) {
        token = token.substring(1);//from   w  w  w  . j a  v  a2s.c om
    }
    if (token.endsWith("\"")) {
        token = token.substring(0, token.length() - 1);
    }
    return token;
}

From source file:Main.java

/**
 * Splits specified string at XML space character boundaries (<tt>'\t'</tt>,
 * <tt>'\r'</tt>, <tt>'\n'</tt>, <tt>' '</tt>). Returns list of
 * parts.// w  w  w .j a  v a  2 s .  c o m
 * 
 * @param s string to be split
 * @return list of parts
 */
public static final String[] splitList(String s) {
    StringTokenizer tokens = new StringTokenizer(s, " \n\r\t");
    String[] split = new String[tokens.countTokens()];

    for (int i = 0; i < split.length; ++i)
        split[i] = tokens.nextToken();

    return split;
}

From source file:Util.java

/** Parse comma-separated list of ints and return as array. */
public static int[] splitInts(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);
    }/*from  w  w  w.  j ava  2  s.  c  o  m*/
    return list;
}

From source file:Main.java

public static long[] strToArrayOfLong(String str) {
    StringTokenizer tmpst = new StringTokenizer(str, "|", false);
    int len = tmpst.countTokens();
    long[] retl = new long[len];

    for (int i = 0; i < len; i++) {
        retl[i] = Long.parseLong(tmpst.nextToken());
    }/*from   w w w .  ja v  a  2s.com*/

    return retl;
}

From source file:net.sf.jabref.exporter.layout.WSITools.java

/**
 * @param  vcr       {@link java.util.Vector} of <tt>String</tt>
 * @param  buf       Description of the Parameter
 * @param  delimstr  Description of the Parameter
 * @return           Description of the Return Value
 *//*from   ww w . java2s .com*/
public static boolean tokenize(Vector<String> vcr, String buf, String delimstr) {
    vcr.clear();
    buf = buf + '\n';

    StringTokenizer st = new StringTokenizer(buf, delimstr);

    while (st.hasMoreTokens()) {
        vcr.add(st.nextToken());
    }

    return true;
}

From source file:Util.java

/** Parse comma-separated list of longs and return as array. */
public static long[] splitLongs(String str) throws IllegalArgumentException {
    StringTokenizer tokenizer = new StringTokenizer(str, ",");
    int n = tokenizer.countTokens();
    long[] list = new long[n];
    for (int i = 0; i < n; i++) {
        String token = tokenizer.nextToken();
        list[i] = Long.parseLong(token);
    }//from w  w  w  .jav a  2s. c o m
    return list;
}