Example usage for java.util StringTokenizer countTokens

List of usage examples for java.util StringTokenizer countTokens

Introduction

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

Prototype

public int countTokens() 

Source Link

Document

Calculates the number of times that this tokenizer's nextToken method can be called before it generates an exception.

Usage

From source file:Main.java

private static String getPathBeforeLastInPathElement(String path) {
    StringTokenizer st = new StringTokenizer(path, ".");
    StringBuffer sb = new StringBuffer();
    int cpt = st.countTokens();
    //      if (cpt==1)
    //         return root;
    int i = cpt - 1;
    for (int j = 0; j < i; j++) {
        String token = st.nextToken();
        sb.append(token);/*from w ww . jav a 2s . c  om*/
        if (j != i - 1)
            sb.append(".");
    }
    return sb.toString();
    //      String last = getLastInPathElement(path);
    //      path = StringUtils.substringBefore(path, last);
    //      return 
}

From source file:com.vmware.identity.idm.STSSpnValidator.java

public static boolean validate(String spn) {
    // validate spn format, and service principal name and domain name are
    // valid strings.
    Validate.notNull(spn, "Service Princiapal Name cannot be null");
    Validate.notEmpty(spn, "Service Principal Name cannot be empty");
    StringTokenizer st = new StringTokenizer(spn, VALID_SPN_SEPARATORS);
    if (st.countTokens() != 2) {
        logAndThrow("Invalid service principal name format: " + spn);
    } else {/*from   ww w. ja  v  a2s .  com*/
        String servicePrincipalName = st.nextToken();
        String domainName = st.nextToken();
        if (null == servicePrincipalName) {
            logAndThrow(String.format("Service Name must be specfied before [%s]" + VALID_SPN_SEPARATORS));
        }
        if (!servicePrincipalName.equalsIgnoreCase(SERVICE_PRINCIPAL_NAME)) {
            logAndThrow("Service name must be STS (case insensitive)");
        }
        if (null == domainName || domainName.trim().isEmpty()) {
            logAndThrow(String.format("Domain Name must be specfied after [%s]" + VALID_SPN_SEPARATORS));
        }
    }
    return true;
}

From source file:kaleidoscope.util.HttpUtils.java

public static Locale getLocale(String acceptLanguage) {
    Locale locale = null;//from  w w w.j a v a  2 s .  co m

    if (StringUtils.isEmpty(acceptLanguage) != true) {
        StringTokenizer langToken = new StringTokenizer(acceptLanguage, ",; ");
        String language = langToken.nextToken().replace('-', '_');

        StringTokenizer localeToken = new StringTokenizer(language, "_");
        switch (localeToken.countTokens()) {
        case 1:
            locale = new Locale(localeToken.nextToken());
            break;
        case 2:
            locale = new Locale(localeToken.nextToken(), localeToken.nextToken());
            break;
        case 3:
            locale = new Locale(localeToken.nextToken(), localeToken.nextToken(), localeToken.nextToken());
            break;
        }
    }

    if (locale == null) {
        locale = Locale.getDefault();
    }

    return locale;
}

From source file:Main.java

/**
 * get the  text string in an element (eg interspersed between child elements), 
 * or "" if there is none or if the Element is null.
 * Tries to ignore white space text; but does not succeed.
 *///from   w w w. j  a  va  2s .c  om
public static String getText(Element el) {
    String res = "";
    if (el != null)
        try {
            el.normalize(); // does not help recognise white space
            NodeList nodes = el.getChildNodes();
            for (int i = 0; i < nodes.getLength(); i++)
                if (nodes.item(i) instanceof Text) {
                    Text text = (Text) nodes.item(i);
                    // this filter seems to make no difference
                    if (!text.isElementContentWhitespace()) {
                        String tData = text.getData();
                        // this seems to be an effective way to catch pure white space
                        StringTokenizer nonWhiteSpace = new StringTokenizer(tData, "\n \t");
                        if (nonWhiteSpace.countTokens() > 0)
                            res = res + tData;
                    }
                }
        } catch (Exception e) {
            System.out.println("Text failure: " + e.getMessage());
        }
    return res;
}

From source file:Main.java

/**
 * Splits a string around matches of the given delimiter character.
 *
 * Where applicable, this method can be used as a substitute for
 * <code>String.split(String regex)</code>, which is not available
 * on a JSR169/Java ME platform.//from   w  w w  . ja  v  a  2  s . co m
 *
 * @param str the string to be split
 * @param delim the delimiter
 * @throws NullPointerException if str is null
 */
static public String[] split(String str, char delim) {
    if (str == null) {
        throw new NullPointerException("str can't be null");
    }

    // Note the javadoc on StringTokenizer:
    //     StringTokenizer is a legacy class that is retained for
    //     compatibility reasons although its use is discouraged in
    //     new code.
    // In other words, if StringTokenizer is ever removed from the JDK,
    // we need to have a look at String.split() (or java.util.regex)
    // if it is supported on a JSR169/Java ME platform by then.
    StringTokenizer st = new StringTokenizer(str, String.valueOf(delim));
    int n = st.countTokens();
    String[] s = new String[n];
    for (int i = 0; i < n; i++) {
        s[i] = st.nextToken();
    }
    return s;
}

From source file:Main.java

/**
 * Creates a list of strings from a string within tokens. Empty tokens will
 * be omitted: If a string within tokens is <code>"a,,b,,c"</code> and the
 * delimiter string is <code>","</code>, the returned list of strings
 * contains the tree elements <code>"a", "b", "c"</code>.
 *
 * @param string    String within tokens
 * @param delimiter Delimiter that separates the tokens. Every character
 *                  of the delimiter string is a separate delimiter. If
 *                  the string within tokens is <code>"I,like:ice"</code>
 *                  and the delimiter string is <code>",:"</code>, the
 *                  returned list of strings contains the three elements
 *                  <code>"I", "like", "ice"</code>.
 * @return          List of strings/*w w w.  j  a  v a2 s.c o  m*/
 */
public static List<String> stringTokenToList(String string, String delimiter) {
    if (string == null) {
        throw new NullPointerException("string == null");
    }

    if (delimiter == null) {
        throw new NullPointerException("delimiter == null");
    }

    StringTokenizer tokenizer = new StringTokenizer(string, delimiter);
    List<String> list = new ArrayList<>(tokenizer.countTokens());

    while (tokenizer.hasMoreTokens()) {
        list.add(tokenizer.nextToken());
    }

    return list;
}

From source file:com.spotify.scio.extra.transforms.ProcessUtil.java

static String[] tokenizeCommand(String command) {
    StringTokenizer st = new StringTokenizer(command);
    String[] cmdArray = new String[st.countTokens()];
    for (int i = 0; st.hasMoreTokens(); i++)
        cmdArray[i] = st.nextToken();// www  .  j a  v a 2s  .  c om
    return cmdArray;
}

From source file:IPAddress.java

/**
 * Check if the specified address is a valid numeric TCP/IP address
 * // w  w  w.  jav a 2  s . c  o  m
 * @param ipaddr String
 * @return boolean
 */
public final static boolean isNumericAddress(String ipaddr) {

    //   Check if the string is valid

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

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

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

    while (token.hasMoreTokens()) {

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

        String ipNum = token.nextToken();

        try {
            int ipVal = Integer.valueOf(ipNum).intValue();
            if (ipVal < 0 || ipVal > 255)
                return false;
        } catch (NumberFormatException ex) {
            return false;
        }
    }

    //   Looks like a valid IP address

    return true;
}

From source file:Main.java

public static final String[] toStringArray(String text, String token) {
    if (text == null || text.length() == 0) {
        return new String[0];
    }/*from   w  w  w .  ja  v  a  2s .co m*/
    StringTokenizer tokens = new StringTokenizer(text, token);
    String[] words = new String[tokens.countTokens()];
    for (int i = 0; i < words.length; i++) {
        words[i] = tokens.nextToken();
    }
    return words;
}

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  ww  .  j av a 2  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);
}