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

/**
 * Change the extension of specified file.<br>
 * Usage :<br>//from   w  w  w  . ja v  a  2 s .c om
 * 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

/**
 * /*from   ww  w. jav  a2s. c  om*/
 * @param value
 * @return int[]
 */
public static int[] toIntArray(final String value) {
    if (value == null) {
        return new int[] {};
    }

    final String strippedValue = value.replace(ARRAY_OPEN_TAG, EMPTY_STRING).replace(ARRAY_CLOSE_TAG,
            EMPTY_STRING);
    final StringTokenizer tokenizer = new StringTokenizer(strippedValue, ELEMENT_SEPARATOR);
    final Collection<Integer> intCollection = new ArrayList<>();

    while (tokenizer.hasMoreTokens()) {
        intCollection.add(Integer.valueOf(tokenizer.nextToken().trim()));
    }

    return toIntArray(intCollection);
}

From source file:Main.java

/**
 * // w  ww.j a v  a  2s.c om
 * @param value
 * @return boolean[]
 */
public static boolean[] toBooleanArray(final String value) {
    if (value == null) {
        return new boolean[] {};
    }

    final String strippedValue = value.replace(ARRAY_OPEN_TAG, EMPTY_STRING).replace(ARRAY_CLOSE_TAG,
            EMPTY_STRING);
    final StringTokenizer tokenizer = new StringTokenizer(strippedValue, ELEMENT_SEPARATOR);
    final Collection<Boolean> intCollection = new ArrayList<>();

    while (tokenizer.hasMoreTokens()) {
        intCollection.add(Boolean.valueOf(tokenizer.nextToken().trim()));
    }

    return toBooleanArray(intCollection);
}

From source file:edu.uci.ics.jung.io.PartitionDecorationReader.java

/**
 * Decorates vertices in the specified partition with typed count data. 
 * The data must be contained in a text file in the following format:
 * /*from   www.  j  av a2s.  c o  m*/
 * <pre>
 * vid_1    type_1  count_1
 * vid_2    type_2  count_2
 * ...
 * </pre>
 * 
 * <p>where <code>count_i</code> (an integer value) represents 
 * the number of elements of 
 * type <code>type_i</code> possessed by the vertex with ID 
 * <code>vid_i</code> (as defined by <code>BipartiteGraphReader.load()</code>)
 * for the <code>i</code>th line in the file.</p>
 * 
 * <p>For example, the vertices might represent authors, the type might
 * represent a topic, and the count might represent the number of 
 * papers that the specified author had written on that topic.<p>
 * 
 * <p>If <code>normalize</code> is <code>true</code>, then the 
 * count data will be scaled so that the counts for
 * each vertex will sum to 1.  (In this case, each vertex must have
 * a positive total count value.)
 * 
 * <p>The end of the file may be artificially set by putting the string
 * <code>end_of_file</code> on a line by itself.</p>
 * 
 * @return              the total number of types observed
 * @param bg            the bipartite graph whose vertices are to be decorated
 * @param count_reader  the reader containing the decoration data
 * @param partition     the partition whose decorations are specified by this file
 * @param count_key     the user key for the decorations
 * @param copyact       the copy action for the decorations
 */
public static int loadCounts(Graph bg, Reader count_reader, Predicate partition, Object count_key,
        UserData.CopyAction copyact) {
    StringLabeller id_label = StringLabeller.getLabeller(bg, partition);
    Set types = new HashSet();
    try {
        BufferedReader br = new BufferedReader(count_reader);

        while (br.ready()) {
            String curLine = br.readLine();
            if (curLine == null || curLine.equals("end_of_file"))
                break;
            if (curLine.trim().length() == 0)
                continue;

            StringTokenizer st = new StringTokenizer(curLine);
            String entity_id = st.nextToken();
            String type_id = st.nextToken();
            Integer count = new Integer(st.nextToken());

            types.add(type_id);

            Vertex v = id_label.getVertex(entity_id);

            if (v == null)
                throw new IllegalArgumentException("Unrecognized vertex " + entity_id);

            Map count_map = (Map) v.getUserDatum(count_key);
            if (count_map == null) {
                count_map = new HashMap();
                v.addUserDatum(count_key, count_map, copyact);
            }
            count_map.put(type_id, count);

        }
        br.close();
        count_reader.close();
    } catch (IOException ioe) {
        throw new FatalException("Error in loading counts from " + count_reader);
    }

    return types.size();
}

From source file:Main.java

/**
 * Find the xpath element under the given root. As the xpath requirement is very
 * simple, so we avoid using XPath API//  ww  w  .  j a  va2s.com
 * @param root - the root element that search begins
 * @param xpath - the path from the root
 * @return - the xpath defined element under the given root.
 */
public static Element findXPathElement(Element root, String xpath) {
    if (root == null)
        return null;
    if (xpath == null || xpath.trim().equals(""))
        return root;
    xpath = toRelativePath(xpath, root.getNodeName());
    StringTokenizer st = new StringTokenizer(xpath, "/", false);
    String sitem;
    Element item = root;
    NodeList list;

    boolean first = true;
    while (st.hasMoreTokens()) {
        sitem = st.nextToken();
        if (first && sitem.equals(item.getNodeName())) {
            first = false;
        } else {
            list = item.getElementsByTagName(sitem);
            if (list.getLength() < 1)
                return null;
            item = (Element) (list.item(0));
        }
    }

    return item;
}

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

protected static String[] asArray(String s) {
    if (s == null) {
        return new String[0];
    }// ww  w .  ja va 2  s  .  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:net.rim.ejde.internal.legacy.Util.java

static public List<String> getSources(Project proj) {
    List<String> sources = new ArrayList<String>();
    String udata = proj.getUserData();

    StringTokenizer st = new StringTokenizer(udata, "|");
    String token;/* www. jav  a 2 s  .  co m*/

    while (st.hasMoreElements()) {
        token = st.nextToken();
        if (StringUtils.isNotBlank(token)) {
            sources.add(token);
        }
    }

    return sources;
}

From source file:org.apache.hive.service.auth.HttpAuthUtils.java

/**
 * Splits the cookie token into attributes pairs.
 * @param str input token./* w  w  w .ja  v a  2s  .c o 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:net.sf.zekr.engine.search.SearchScopeItem.java

/**
 * Creates a SearchScopeItem from the given string <code>scopeItemStr</code>. A string representation of a
 * search scope item is of the form <tt>[-]sura_from,aya_from,sura_to,aya_to</tt>. "<tt>-</tt>" is used to
 * specify that this search scope is <b>exclusive</b>.
 * /*from www  . ja v a  2 s.co  m*/
 * @param scopeItemStr input scope item in string format
 * @return a new <code>SearchScopeItem</code> instance
 * @throws IllegalSearchScopeItemException if scope is not in well format
 */
public static SearchScopeItem deserialize(String scopeItemStr) {
    StringTokenizer t = new StringTokenizer(scopeItemStr, DELIM + " \n\r");
    int sf, af, st, at;
    try {
        sf = Integer.parseInt(t.nextToken());
        af = Integer.parseInt(t.nextToken());
        st = Integer.parseInt(t.nextToken());
        at = Integer.parseInt(t.nextToken());
    } catch (NumberFormatException e) {
        throw new IllegalSearchScopeItemException();
    }
    return new SearchScopeItem(Math.abs(sf), af, st, at, sf < 0);
}

From source file:com.bdaum.zoom.gps.internal.GpsUtilities.java

public static void getGeoAreas(IPreferenceStore preferenceStore, Collection<GeoArea> areas) {
    NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
    nf.setMaximumFractionDigits(8);//from w ww .j  a  va2s.c o  m
    nf.setGroupingUsed(false);
    String nogo = preferenceStore.getString(PreferenceConstants.NOGO);
    if (nogo != null) {
        int i = 0;
        double lat = 0, lon = 0, km = 0;
        String name = null;
        StringTokenizer st = new StringTokenizer(nogo, ";,", true); //$NON-NLS-1$
        while (st.hasMoreTokens()) {
            String s = st.nextToken();
            if (";".equals(s)) { //$NON-NLS-1$
                areas.add(new GeoArea(name, lat, lon, km));
                i = 0;
            } else if (",".equals(s)) //$NON-NLS-1$
                ++i;
            else
                try {
                    switch (i) {
                    case 0:
                        name = s;
                        break;
                    case 1:
                        lat = nf.parse(s).doubleValue();
                        break;
                    case 2:
                        lon = nf.parse(s).doubleValue();
                        break;
                    case 3:
                        km = nf.parse(s).doubleValue();
                        break;
                    }
                } catch (ParseException e) {
                    // do nothing
                }
        }
    }
}