Example usage for java.util StringTokenizer hasMoreTokens

List of usage examples for java.util StringTokenizer hasMoreTokens

Introduction

In this page you can find the example usage for java.util StringTokenizer hasMoreTokens.

Prototype

public boolean hasMoreTokens() 

Source Link

Document

Tests if there are more tokens available from this tokenizer's string.

Usage

From source file:IPAddress.java

/**
 * Check if the specified address is a valid numeric TCP/IP address and return as an integer value
 * //from w  ww. j  a v  a  2 s.c o  m
 * @param ipaddr String
 * @return int
 */
public final static int parseNumericAddress(String ipaddr) {

    //   Check if the string is valid

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

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

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

    int ipInt = 0;

    while (token.hasMoreTokens()) {

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

        String ipNum = token.nextToken();

        try {

            //   Validate the current address part

            int ipVal = Integer.valueOf(ipNum).intValue();
            if (ipVal < 0 || ipVal > 255)
                return 0;

            //   Add to the integer address

            ipInt = (ipInt << 8) + ipVal;
        } catch (NumberFormatException ex) {
            return 0;
        }
    }

    //   Return the integer address

    return ipInt;
}

From source file:com.buaa.cfs.security.ShellBasedUnixGroupsMapping.java

/**
 * Get the current user's group list from Unix by running the command 'groups' NOTE. For non-existing user it will
 * return EMPTY list//from  www .  j a  v  a2  s  .  c o  m
 *
 * @param user user name
 *
 * @return the groups list that the <code>user</code> belongs to. The primary group is returned first.
 *
 * @throws IOException if encounter any error when running the command
 */
private static List<String> getUnixGroups(final String user) throws IOException {
    String result = "";
    try {
        result = Shell.execCommand(Shell.getGroupsForUserCommand(user));
    } catch (Shell.ExitCodeException e) {
        // if we didn't get the group - just return empty list;
        LOG.warn("got exception trying to get groups for user " + user + ": " + e.getMessage());
        return new LinkedList<String>();
    }

    StringTokenizer tokenizer = new StringTokenizer(result, Shell.TOKEN_SEPARATOR_REGEX);
    List<String> groups = new LinkedList<String>();
    while (tokenizer.hasMoreTokens()) {
        groups.add(tokenizer.nextToken());
    }

    // remove duplicated primary group
    if (!Shell.WINDOWS) {
        for (int i = 1; i < groups.size(); i++) {
            if (groups.get(i).equals(groups.get(0))) {
                groups.remove(i);
                break;
            }
        }
    }

    return groups;
}

From source file:fr.cls.atoll.motu.library.converter.jaxb.LocaleAdapter.java

/**
 * Tokenize the given String into a String array via a StringTokenizer.
 * <p>/*  ww w  .ja  v a  2 s  . c  o m*/
 * The given delimiters string is supposed to consist of any number of delimiter characters. Each of those
 * characters can be used to separate tokens. A delimiter is always a single character; for
 * multi-character delimiters, consider using <code>delimitedListToStringArray</code>
 * 
 * @param str the String to tokenize
 * @param delimiters the delimiter characters, assembled as String (each of those characters is
 *            individually considered as delimiter)
 * @param trimTokens trim the tokens via String's <code>trim</code>
 * @param ignoreEmptyTokens omit empty tokens from the result array (only applies to tokens that are empty
 *            after trimming; StringTokenizer will not consider subsequent delimiters as token in the
 *            first place).
 * @return an array of the tokens (<code>null</code> if the input String was <code>null</code>)
 * @see java.util.StringTokenizer
 * @see java.lang.String#trim()
 * @see #delimitedListToStringArray
 */
private static String[] tokenizeToStringArray(String str, String delimiters, boolean trimTokens,
        boolean ignoreEmptyTokens) {

    if (str == null) {
        return null;
    }
    StringTokenizer st = new StringTokenizer(str, delimiters);
    List tokens = new ArrayList();
    while (st.hasMoreTokens()) {
        String token = st.nextToken();
        if (trimTokens) {
            token = token.trim();
        }
        if (!ignoreEmptyTokens || token.length() > 0) {
            tokens.add(token);
        }
    }
    return (String[]) tokens.toArray(new String[tokens.size()]);
}

From source file:Main.java

/**
 * Change the extension of specified file.<br>
 * Usage :<br>/*from w  ww. jav  a  2  s.co m*/
 * f.renameTo(MyFileUtility.reExtension(f, "jpg")) ;<br>
 * As we know ,File class is a dummy File , <br>
 * thus , you must follow the usage to change extension.
 * @param fin File input
 * @param newExtension newExtension without '.'
 * @return File with new extension
 */
public static File reExtension(File fin, String newExtension) {
    //Usage: f.renameTo(MyFileUtility.reExtension(f, "jpg")) ;
    StringTokenizer strTokener = new StringTokenizer(fin.getName(), ".");
    //For a file may has many '.' in its file name , we use a collection to stroe it.
    ArrayList<String> strVec = new ArrayList<String>();
    while (strTokener.hasMoreTokens())
        strVec.add(strTokener.nextToken());

    String newName = "";
    //Give up the original extension
    for (int i = 0; i != strVec.size() - 1; ++i) {
        newName += strVec.get(i);
        newName += ".";
    }
    newName += newExtension;
    return new File(fin.getParent() + "\\" + newName);
}

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

/**
 * Remove an element using XPath-quotation expressions. Path must not including
 * the context element, path elements can be separated by / or .,
 * and namespace is NOT supported./*from  w  ww  .  ja v  a 2s  .c  o  m*/
 * @param context Element to start the search from, cannot be null.
 * @param path XPath-quotation expression, cannot be null.
 * @return the removed element if there are matches, otherwise
 * return null.
 */
public static Element removeElementByPath(Element context, String path) {
    Element cur = context;
    StringTokenizer tokens = new StringTokenizer(path, "/");
    String name = null;
    while (tokens.hasMoreTokens()) {
        name = tokens.nextToken();
        cur = getElementByPath(cur, name);
        if (cur == null) {
            return null;
        }
    }
    if (name != null) {
        Element parent = (Element) cur.getParentNode();
        return removeChildElement(parent, name);
    }
    return null;
}

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 2 s .  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:org.apache.hive.service.auth.HttpAuthUtils.java

/**
 * Splits the cookie token into attributes pairs.
 * @param str input token./*from ww w. j  a  v  a2s  . co m*/
 * @return a map with the attribute pairs of the token if the input is valid.
 * Else, returns null.
 */
private static Map<String, String> splitCookieToken(String tokenStr) {
    Map<String, String> map = new HashMap<String, String>();
    StringTokenizer st = new StringTokenizer(tokenStr, COOKIE_ATTR_SEPARATOR);

    while (st.hasMoreTokens()) {
        String part = st.nextToken();
        int separator = part.indexOf(COOKIE_KEY_VALUE_SEPARATOR);
        if (separator == -1) {
            LOG.error("Invalid token string " + tokenStr);
            return null;
        }
        String key = part.substring(0, separator);
        String value = part.substring(separator + 1);
        map.put(key, value);
    }
    return map;
}

From source file:com.adaptris.core.http.jetty.JettyConnection.java

protected static String[] asArray(String s) {
    if (s == null) {
        return new String[0];
    }//from  ww w  .  j a  va  2s.c  o m
    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);
    }
    return l.toArray(new String[0]);
}

From source file:com.canoo.webtest.plugins.emailtest.AbstractSelectStep.java

static boolean doMatchMultiple(final String expected, final Address[] actuals) {
    // semantics are: if no expectation then match
    if (StringUtils.isEmpty(expected)) {
        return true;
    } else if (actuals == null) {
        return false;
    }/*from ww w .j  a v a  2  s  .  co  m*/

    final StringTokenizer expectedTokens = new StringTokenizer(expected, ",");
    while (expectedTokens.hasMoreTokens()) {
        final String expectedToken = expectedTokens.nextToken().trim();
        boolean hasMatched = false;
        for (int i = 0; i < actuals.length; i++) {
            final Address actual = actuals[i];
            hasMatched = doMatch(expectedToken, actual.toString());
            if (hasMatched) {
                break;
            }
        }
        if (!hasMatched) {
            return false;
        }
    }
    return true;
}