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:jenkins.plugins.testopia.TestopiaBuilder.java

/**
 * Maybe adds a system property if it is in format <key>=<value>.
 * //from ww  w .ja  v a 2 s .com
 * @param systemProperty System property entry in format <key>=<value>.
 * @param listener Jenkins Build listener
 */
public static void maybeAddSystemProperty(String systemProperty, BuildListener listener) {
    final StringTokenizer tokenizer = new StringTokenizer(systemProperty, "=:");
    if (tokenizer.countTokens() == 2) {
        final String key = tokenizer.nextToken();
        final String value = tokenizer.nextToken();

        if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(value)) {
            if (key.contains(BASIC_HTTP_PASSWORD)) {
                listener.getLogger().println(Messages.Testopia_Builder_Password(key));
            } else {
                listener.getLogger().println(Messages.Testopia_Builder_Setting(key, value));
            }
            try {
                System.setProperty(key, value);
            } catch (SecurityException se) {
                se.printStackTrace(listener.getLogger());
            }

        }
    }
}

From source file:org.alfresco.web.app.servlet.BaseServlet.java

/**
 * Resolve a name based into a NodeRef and Filename string
 *  //from   w ww . j a  v a2 s  . c  om
 * @param sc      ServletContext
 * @param path    'cm:name' based path using the '/' character as a separator
 *  
 * @return PathRefInfo structure containing the resolved NodeRef and filename
 * 
 * @throws IllegalArgumentException
 */
public final static PathRefInfo resolveNamePath(ServletContext sc, String path) {
    StringTokenizer t = new StringTokenizer(path, "/");
    int tokenCount = t.countTokens();
    String[] elements = new String[tokenCount];
    for (int i = 0; i < tokenCount; i++) {
        elements[i] = t.nextToken();
    }

    // process name based path tokens using the webdav path resolving helper 
    NodeRef nodeRef = resolveWebDAVPath(sc, elements, false);
    if (nodeRef == null) {
        // unable to resolve path - output helpful error to the user
        throw new IllegalArgumentException("Unable to resolve item Path: " + path);
    }

    return new PathRefInfo(nodeRef, elements[tokenCount - 1]);
}

From source file:Main.java

/**
 * <p>//  ww w . ja v  a 2 s  .co  m
 * Splits the provided text into a array, based on a given separator.
 * </p>
 * <p/>
 * <p>
 * The separator is not included in the returned String array. The maximum number of splits to perfom can be
 * controlled. A <code>null</code> separator will cause parsing to be on whitespace.
 * </p>
 * <p/>
 * <p>
 * This is useful for quickly splitting a String directly into an array of tokens, instead of an enumeration of
 * tokens (as <code>StringTokenizer</code> does).
 * </p>
 *
 * @param str The string to parse.
 * @param separator Characters used as the delimiters. If <code>null</code>, splits on whitespace.
 * @param max The maximum number of elements to parse. The rest of the string to parse will be contained in the last
 * array element. A zero or negative value implies no limit.
 * @return an array of parsed Strings
 */
public static String[] split(String str, String separator, int max) {
    StringTokenizer tok = null;
    if (separator == null) {
        // Null separator means we're using StringTokenizer's default
        // delimiter, which comprises all whitespace characters.
        tok = new StringTokenizer(str);
    } else {
        tok = new StringTokenizer(str, separator);
    }

    int listSize = tok.countTokens();
    if ((max > 0) && (listSize > max)) {
        listSize = max;
    }

    String[] list = new String[listSize];
    int i = 0;
    int lastTokenBegin = 0;
    int lastTokenEnd = 0;
    while (tok.hasMoreTokens()) {
        if ((max > 0) && (i == listSize - 1)) {
            // In the situation where we hit the max yet have
            // tokens left over in our input, the last list
            // element gets all remaining text.
            String endToken = tok.nextToken();
            lastTokenBegin = str.indexOf(endToken, lastTokenEnd);
            list[i] = str.substring(lastTokenBegin);
            break;
        } else {
            list[i] = tok.nextToken();
            lastTokenBegin = str.indexOf(list[i], lastTokenEnd);
            lastTokenEnd = lastTokenBegin + list[i].length();
        }
        i++;
    }
    return list;
}

From source file:edu.stanford.muse.index.NEROld.java

public static void readLocationsFreebase() throws IOException {
    InputStream is = new GZIPInputStream(NER.class.getClassLoader().getResourceAsStream("locations.gz"));
    LineNumberReader lnr = new LineNumberReader(new InputStreamReader(is, "UTF-8"));
    while (true) {
        String line = lnr.readLine();
        if (line == null)
            break;
        StringTokenizer st = new StringTokenizer(line, "\t");
        if (st.countTokens() == 3) {
            String locationName = st.nextToken();
            String canonicalName = locationName.toLowerCase();
            String lat = st.nextToken();
            String longi = st.nextToken();
            locations.put(canonicalName, new LocationInfo(locationName, lat, longi));
        }//from  w  ww  .j  av  a  2 s .  c om
    }
}

From source file:com.qframework.core.ServerkoParse.java

public static int[] parseColorVector(String data) {
    int val = 0;
    StringTokenizer tok = new StringTokenizer(data, ",");
    int len = tok.countTokens();
    int[] array = new int[len];
    int count = 0;

    while (tok.hasMoreTokens())
        try {/*w w w  .  java2  s . co m*/
            val = ColorFactory.getColorVal(tok.nextToken());
            array[count++] = val;
        } catch (NoSuchElementException e) {
        }

    return array;
}

From source file:com.qframework.core.ServerkoParse.java

public static float[] parseFloatVector(String data) {
    float val = 0;
    StringTokenizer tok = new StringTokenizer(data, ",");
    int len = tok.countTokens();
    float[] array = new float[len];
    int count = 0;
    while (tok.hasMoreTokens())
        try {/*from   w  w w  . j  a v a  2  s  .c om*/
            try {
                val = Float.parseFloat(tok.nextToken());
            } catch (NumberFormatException e) {
            }
            array[count++] = val;
        } catch (NoSuchElementException e) {
        }

    return array;
}

From source file:com.qframework.core.ServerkoParse.java

public static int[] parseIntVector(String data) {
    int val = 0;
    StringTokenizer tok = new StringTokenizer(data, ",");
    int len = tok.countTokens();
    int[] array = new int[len];
    int count = 0;

    while (tok.hasMoreTokens())
        try {//from   www .j  a v a2 s . com
            try {
                val = Integer.parseInt(tok.nextToken());
            } catch (NumberFormatException e) {
            }
            array[count++] = val;
        } catch (NoSuchElementException e) {
        }

    return array;
}

From source file:edu.stanford.muse.index.NEROld.java

public static void readLocationsWG() {
    try {/*from   www  . j  av a2  s .co  m*/
        InputStream is = new GZIPInputStream(
                NER.class.getClassLoader().getResourceAsStream("WG.locations.txt.gz"));
        LineNumberReader lnr = new LineNumberReader(new InputStreamReader(is, "UTF-8"));
        while (true) {
            String line = lnr.readLine();
            if (line == null)
                break;
            StringTokenizer st = new StringTokenizer(line, "\t");
            if (st.countTokens() == 4) {
                String locationName = st.nextToken();
                String canonicalName = locationName.toLowerCase();
                if (locationsToSuppress.contains(canonicalName))
                    continue;
                String lat = st.nextToken();
                String longi = st.nextToken();
                String pop = st.nextToken();
                long popl = Long.parseLong(pop);
                float latf = ((float) Integer.parseInt(lat)) / 100.0f;
                float longif = ((float) Integer.parseInt(longi)) / 100.0f;
                Long existingPop = populations.get(canonicalName);
                if (existingPop == null || popl > existingPop) {
                    populations.put(canonicalName, popl);
                    locations.put(canonicalName,
                            new LocationInfo(locationName, Float.toString(latf), Float.toString(longif)));
                }
            }
        }
    } catch (Exception e) {
        log.warn("Unable to read World Gazetteer file, places info may be inaccurate");
        log.debug(Util.stackTrace(e));
    }
}

From source file:info.evanchik.eclipse.karaf.core.KarafCorePluginUtils.java

private static String nextLocation(final StringTokenizer st) {
    String retVal = null;// w  ww.  ja v a  2  s.  c om

    if (st.countTokens() > 0) {
        String tokenList = "\" ";
        StringBuffer tokBuf = new StringBuffer(10);
        String tok = null;
        boolean inQuote = false;
        boolean tokStarted = false;
        boolean exit = false;
        while (st.hasMoreTokens() && !exit) {
            tok = st.nextToken(tokenList);
            if (tok.equals("\"")) {
                inQuote = !inQuote;
                if (inQuote) {
                    tokenList = "\"";
                } else {
                    tokenList = "\" ";
                }

            } else if (tok.equals(" ")) {
                if (tokStarted) {
                    retVal = tokBuf.toString();
                    tokStarted = false;
                    tokBuf = new StringBuffer(10);
                    exit = true;
                }
            } else {
                tokStarted = true;
                tokBuf.append(tok.trim());
            }
        }

        // Handle case where end of token stream and
        // still got data
        if (!exit && tokStarted) {
            retVal = tokBuf.toString();
        }
    }

    return retVal;
}

From source file:info.evanchik.eclipse.karaf.core.KarafCorePluginUtils.java

/**
 * Loads a configuration file relative to the specified base directory. This
 * method also processes any include directives that import other properties
 * files relative to the specified property file.
 *
 * @param base/*w  w  w .j  a  v a 2  s.  co  m*/
 *            the directory containing the file
 * @param filename
 *            the relative path to the properties file
 * @param processIncludes
 *            true if {@link #INCLUDES_PROPERTY} statements should be
 *            followed; false otherwise.
 * @return the {@link Properties} object created from the contents of
 *         configuration file
 * @throws CoreException
 *             if there is a problem loading the file
 */
public static Properties loadProperties(final File base, final String filename, final boolean processIncludes)
        throws CoreException {
    final File f = new File(base, filename);

    try {
        final Properties p = new Properties();
        p.load(new FileInputStream(f));

        final String includes = p.getProperty(INCLUDES_PROPERTY);
        if (includes != null) {
            final StringTokenizer st = new StringTokenizer(includes, "\" ", true);
            if (st.countTokens() > 0) {
                String location;
                do {
                    location = nextLocation(st);
                    if (location != null) {
                        final Properties includeProps = loadProperties(base, location);
                        p.putAll(includeProps);
                    }
                } while (location != null);
            }
            p.remove(INCLUDES_PROPERTY);
        }

        return p;
    } catch (final IOException e) {
        final String message = "Unable to load configuration file from configuration directory: "
                + f.getAbsolutePath();
        throw new CoreException(
                new Status(IStatus.ERROR, KarafCorePluginActivator.PLUGIN_ID, IStatus.OK, message, e));
    }

}