Example usage for java.util StringTokenizer StringTokenizer

List of usage examples for java.util StringTokenizer StringTokenizer

Introduction

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

Prototype

public StringTokenizer(String str, String delim) 

Source Link

Document

Constructs a string tokenizer for the specified string.

Usage

From source file:gov.nih.nci.lv.util.LVUtils.java

/**
 * converts a ids seperated by a comma to a set.
 * @param ids ids/*from w ww  . j  av  a 2 s .  com*/
 * @param delimiter delimiter
 * @return Set
 */
public static List<Long> convertStringToList(String ids, String delimiter) {
    List<Long> labs = new ArrayList<Long>();
    if (StringUtils.isEmpty(ids)) {
        return labs;
    }
    StringTokenizer st = new StringTokenizer(ids, delimiter);
    while (st.hasMoreTokens()) {
        labs.add(new Long(st.nextElement().toString()));
    }
    return labs;
}

From source file:Main.java

public static int findProcessIdWithPS(String command) throws Exception {

    int procId = -1;

    Runtime r = Runtime.getRuntime();

    Process procPs = null;//from   w  w  w  . j a va  2s. co  m

    procPs = r.exec(SHELL_CMD_PS);

    BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream()));
    String line = null;

    while ((line = reader.readLine()) != null) {
        if (line.indexOf(' ' + command) != -1) {

            StringTokenizer st = new StringTokenizer(line, " ");
            st.nextToken(); // proc owner

            procId = Integer.parseInt(st.nextToken().trim());

            break;
        }
    }

    return procId;

}

From source file:com.amalto.core.history.accessor.record.DataRecordAccessor.java

@SuppressWarnings("rawtypes")
private static LinkedList<PathElement> getPath(DataRecord dataRecord, String path) {
    LinkedList<PathElement> elements = new LinkedList<PathElement>();
    StringTokenizer tokenizer = new StringTokenizer(path, "/"); //$NON-NLS-1$
    DataRecord current = dataRecord;//  ww w . j av a2  s.c o  m
    while (tokenizer.hasMoreElements()) {
        String element = tokenizer.nextToken();
        PathElement pathElement = new PathElement();
        if (element.indexOf('@') == 0) {
            pathElement.field = elements.getLast().field;
            pathElement.setter = TypeValue.SET;
            pathElement.getter = TypeValue.GET;
        } else {
            if (current == null) {
                throw new IllegalStateException("Cannot update '" + path + "'."); //$NON-NLS-1$ //$NON-NLS-2$
            }
            if (element.indexOf('[') > 0) {
                pathElement.field = current.getType().getField(StringUtils.substringBefore(element, "[")); //$NON-NLS-1$
                if (!pathElement.field.isMany()) {
                    throw new IllegalStateException(
                            "Expected a repeatable field for '" + element + "' in path '" + path //$NON-NLS-1$ //$NON-NLS-2$
                                    + "'."); //$NON-NLS-1$
                }
                int indexStart = element.indexOf('[');
                int indexEnd = element.indexOf(']');
                if (indexStart < 0 || indexEnd < 0) {
                    throw new RuntimeException(
                            "Field name '" + element + "' did not match many field pattern in path '" //$NON-NLS-1$ //$NON-NLS-2$
                                    + path + "'."); //$NON-NLS-1$
                }
                pathElement.index = Integer.parseInt(element.substring(indexStart + 1, indexEnd)) - 1;
                pathElement.setter = ManyValue.SET;
                pathElement.getter = ManyValue.GET;
                List list = (List) current.get(pathElement.field);
                if (list == null || pathElement.index > list.size() - 1) {
                    throw new IllegalStateException("Cannot update '" + path + "'."); //$NON-NLS-1$ //$NON-NLS-2$
                }
                Object value = list.get(pathElement.index);
                if (value instanceof DataRecord) {
                    current = (DataRecord) value;
                } else if (value instanceof List) {
                    throw new IllegalStateException();
                }
            } else {
                pathElement.field = current.getType().getField(element);
                pathElement.setter = SimpleValue.SET;
                pathElement.getter = SimpleValue.GET;
                if (pathElement.field instanceof ContainedTypeFieldMetadata
                        || pathElement.field instanceof ReferenceFieldMetadata) {
                    Object value = current.get(pathElement.field);
                    if (value instanceof DataRecord) {
                        current = (DataRecord) value;
                    }
                }
            }
        }
        elements.add(pathElement);
    }
    return elements;
}

From source file:com.salesmanager.core.util.MerchantConfigurationUtil.java

public static IntegrationProperties getIntegrationProperties(String configurationValue, String delimiter) {
    if (configurationValue == null)
        return new IntegrationProperties();
    StringTokenizer st = new StringTokenizer(configurationValue, delimiter);
    int i = 1;/* ww w. j av  a 2  s. c  o m*/
    IntegrationProperties keys = new IntegrationProperties();
    while (st.hasMoreTokens()) {
        String value = st.nextToken();
        if (i == 1) {
            keys.setProperties1(value);
        } else if (i == 2) {
            keys.setProperties2(value);
        } else if (i == 3) {
            keys.setProperties3(value);
        } else if (i == 4) {
            keys.setProperties4(value);
        } else {
            keys.setProperties5(value);
        }

        i++;
    }
    return keys;
}

From source file:aiai.ai.yaml.env.TimePeriods.java

public static TimePeriods from(String s) {
    if (StringUtils.isBlank(s)) {
        return ALWAYS_ACTIVE;
    }/*w ww .  j av a 2 s . c  o m*/

    TimePeriods periods = new TimePeriods();
    for (StringTokenizer st = new StringTokenizer(s, ","); st.hasMoreTokens();) {
        String token = st.nextToken().trim();
        periods.periods.add(asTimePeriod(token));
    }
    return periods;
}

From source file:com.codecrate.shard.transfer.pcgen.tag.PcgenTags.java

public PcgenTags(String line) {
    StringTokenizer tokens = new StringTokenizer(line, TAG_SEPERATOR);
    while (tokens.hasMoreTokens()) {
        String token = tokens.nextToken();
        int seperatorIndex = token.indexOf(TAG_NAME_VALUE_SEPERATOR);
        if (-1 == seperatorIndex) {
            this.undefinedTagValue = token.trim();
        } else {/*w w w  .  j av a2s . co m*/
            String tagName = token.substring(0, seperatorIndex).trim();
            String tagValue = token.substring(seperatorIndex + 1, token.length()).trim();

            addTagValue(tagName, tagValue);
        }
    }
}

From source file:Main.java

/**
 * Returns a reference to a file with the specified name that is located
 * somewhere on the classpath. The code for this method is an adaptation of
 * code supplied by Dave Postill./* w  w w.jav a2 s . c  o m*/
 * 
 * @param name
 *          the filename.
 * 
 * @return a reference to a file or <code>null if no file could be
 *         found.
 */
public static File findFileOnClassPath(final String name) {

    final String classpath = System.getProperty("java.class.path");
    final String pathSeparator = System.getProperty("path.separator");

    final StringTokenizer tokenizer = new StringTokenizer(classpath, pathSeparator);

    while (tokenizer.hasMoreTokens()) {
        final String pathElement = tokenizer.nextToken();

        final File directoryOrJar = new File(pathElement);
        final File absoluteDirectoryOrJar = directoryOrJar.getAbsoluteFile();

        if (absoluteDirectoryOrJar.isFile()) {
            final File target = new File(absoluteDirectoryOrJar.getParent(), name);
            if (target.exists()) {
                return target;
            }
        } else {
            final File target = new File(directoryOrJar, name);
            if (target.exists()) {
                return target;
            }
        }

    }
    return null;

}

From source file:booknext.DatabaseSetup.java

public void linesToBooks(List<String> lines, MysqlConnection connection) {
    int cont = 0, inserted = 0;
    List<CBook> usedBooks = new ArrayList();
    List<String> genres = new ArrayList();
    List<Integer> genresPoints = new ArrayList();
    for (String line : lines) {
        //ISBN|-|TITULO|-|AUTORES|-|DESCRIPCION|-|GENEROS|-|IMAGEN|-|ANIO|-|PUBLISHER
        CBook book = new CBook();
        List<String> tokens = new ArrayList();
        StringTokenizer token = new StringTokenizer(line, "\t");
        while (token.hasMoreTokens()) {
            tokens.add(token.nextToken());
        }/* w  w  w . j av  a2 s . c om*/
        String[] contents = new String[tokens.size()];
        for (int i = 0; i < tokens.size(); i++) {
            contents[i] = tokens.get(i);
        }
        //String[] contents = line.split("\\|-|");
        book.setBookId(contents[0]);
        book.isbn = contents[0];
        book.setBook_name(contents[1]);

        List<String> lAuthors = new ArrayList();
        StringTokenizer token2 = new StringTokenizer(contents[2], "|");
        while (token2.hasMoreTokens()) {
            lAuthors.add(token2.nextToken());
        }
        book.setBook_authors(lAuthors);

        contents[3] = contents[3].replace("|", " | ");
        if (contents[3].equals("NODESCRIPTION")) {
            book.setBook_description("");
        } else {
            book.setBook_description(contents[3]);
        }

        boolean shouldInsert = true;
        contents[4] = contents[4].replace("|", ", ");
        if (contents[4].equals("NOGENRE")) {
            shouldInsert = false;
        } else {
            book.setBook_genre(contents[4]);
        }

        if (!contents[5].equals("NOIMAGE")) {
            book.setBook_image(contents[5]);
        } else {
            book.setBook_image("/Icons/No_Cover.jpg");
        }

        book.setBook_publishYear(contents[6]);

        book.setBook_publisher(contents[7]);

        List<String> ignoredGenres = new ArrayList();
        ignoredGenres.add("Protected DAISY");
        ignoredGenres.add("Accessible book");
        ignoredGenres.add("In library");
        ignoredGenres.add("OverDrive");
        ignoredGenres.add("Large type books");
        ignoredGenres.add("Popular Print Disabled Books");
        ignoredGenres.add("Fiction");

        if (shouldInsert) {
            usedBooks.add(book);
            String[] genresForBook = book.getBook_genre().split(", ");
            for (String genreForBook : genresForBook) {
                int index = genres.indexOf(genreForBook);
                if (index == -1) {
                    if (!ignoredGenres.contains(genreForBook)) {
                        genres.add(genreForBook);
                        genresPoints.add(1);
                    }
                } else {
                    genresPoints.set(index, genresPoints.get(index) + 1);
                }
            }
            inserted++;
            System.out.println("Inserted " + String.valueOf(cont) + " - " + book.getBook_name());
        }

        cont++;
    }

    System.out.println("Starting to filter by popular categories");

    genres = BubbleSort(genresPoints, genres);
    int size = 5;
    List<String> finalGenres = new ArrayList();
    for (int i = 0; i < size; i++) {
        finalGenres.add(genres.get(i));
        System.out.println("Popular categorie: " + genres.get(i));
    }

    List<CBook> finalBooks = new ArrayList();

    for (CBook usedBook : usedBooks) {
        String[] genresForBook = usedBook.getBook_genre().split(", ");
        boolean used = false;
        for (String genreForBook : genresForBook) {
            if (!used && finalGenres.contains(genreForBook)) {
                finalBooks.add(usedBook);
                connection.addBook(usedBook);
                used = true;
            }
        }
    }
    System.out.println("Inserted: " + String.valueOf(inserted) + "/" + String.valueOf(cont));
    System.out.println("Fixing descriptions");
    fixDescriptions(finalBooks, connection);
}

From source file:com.npower.dm.util.DDFTreeHelper.java

/**
 * Split the nodePath into a List. The "./" prefix in the node path will be
 * ignored. eg: ./a/b/c/d/ results: {a, b, c, d} /a/b/c/d/ results: {a, b, c,
 * d} a/b/c/d results: {a, b, c, d}//from  w w  w  . jav  a  2s  .com
 * 
 * @param nodePath
 * @return
 */
public static List<String> getPathVector(String nodePath) {
    String path = nodePath;
    if (path.startsWith("./")) {
        path = nodePath.substring(2, path.length());
    }
    StringTokenizer tokenizer = new StringTokenizer(path, "/");
    List<String> pathVector = new Vector<String>();

    while (tokenizer.hasMoreTokens()) {
        pathVector.add(tokenizer.nextToken());
    }
    return pathVector;
}

From source file:com.microsoft.aad.adal.HashMapExtensions.java

/**
 * decode url string into a key value pairs with given query delimiter given
 * string as a=1&b=2 will return key value of [[a,1],[b,2]].
 * /*from  w ww  .  ja  v a 2s. c  o  m*/
 * @param parameters
 * @param delimiter
 * @return key value pairs
 */
static final HashMap<String, String> URLFormDecodeData(String parameters, String delimiter) {
    HashMap<String, String> result = new HashMap<String, String>();

    if (!StringExtensions.IsNullOrBlank(parameters)) {
        StringTokenizer parameterTokenizer = new StringTokenizer(parameters, delimiter);

        while (parameterTokenizer.hasMoreTokens()) {
            String pair = parameterTokenizer.nextToken();
            String[] elements = pair.split("=");

            if (elements != null && elements.length == 2) {
                String key = null;
                String value = null;
                try {
                    key = StringExtensions.URLFormDecode(elements[0].trim());
                    value = StringExtensions.URLFormDecode(elements[1].trim());
                } catch (UnsupportedEncodingException e) {
                    Log.d(TAG, e.getMessage());
                }

                if (!StringExtensions.IsNullOrBlank(key) && !StringExtensions.IsNullOrBlank(value)) {
                    result.put(key, value);
                }
            }
        }
    }

    return result;
}