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:edu.psu.citeseerx.updates.IndexUpdateManager.java

/**
 * Builds a list of author normalizations to create more flexible
 * author search./*w w w .ja  va2  s.  c o  m*/
 * @param names
 * @return
 */
private static List<String> buildAuthorNorms(List<String> names) {
    HashSet<String> norms = new HashSet<String>();
    for (String name : names) {
        name = name.replaceAll("[^\\p{L} ]", "");
        StringTokenizer st = new StringTokenizer(name);
        String[] tokens = new String[st.countTokens()];
        int counter = 0;
        while (st.hasMoreTokens()) {
            tokens[counter] = st.nextToken();
            counter++;
        }
        norms.add(joinStringArray(tokens));

        if (tokens.length > 2) {

            String[] n1 = new String[tokens.length];
            for (int i = 0; i < tokens.length; i++) {
                if (i < tokens.length - 1) {
                    n1[i] = Character.toString(tokens[i].charAt(0));
                } else {
                    n1[i] = tokens[i];
                }
            }

            String[] n2 = new String[tokens.length];
            for (int i = 0; i < tokens.length; i++) {
                if (i > 0 && i < tokens.length - 1) {
                    n2[i] = Character.toString(tokens[i].charAt(0));
                } else {
                    n2[i] = tokens[i];
                }
            }

            norms.add(joinStringArray(n1));
            norms.add(joinStringArray(n2));
        }

        if (tokens.length > 1) {

            String[] n3 = new String[2];
            n3[0] = tokens[0];
            n3[1] = tokens[tokens.length - 1];

            String[] n4 = new String[2];
            n4[0] = Character.toString(tokens[0].charAt(0));
            n4[1] = tokens[tokens.length - 1];

            norms.add(joinStringArray(n3));
            norms.add(joinStringArray(n4));
        }
    }

    ArrayList<String> normList = new ArrayList<String>();
    for (Iterator<String> it = norms.iterator(); it.hasNext();) {
        normList.add(it.next());
    }

    return normList;
}

From source file:com.frameworkset.commons.dbcp2.BasicDataSourceFactory.java

/**
 * Parse list of property values from a delimited string
 * @param value delimited list of values
 * @param delimiter character used to separate values in the list
 * @return String Collection of values/* w w  w.ja v a 2 s  .  com*/
 */
private static Collection<String> parseList(String value, char delimiter) {
    StringTokenizer tokenizer = new StringTokenizer(value, Character.toString(delimiter));
    Collection<String> tokens = new ArrayList<String>(tokenizer.countTokens());
    while (tokenizer.hasMoreTokens()) {
        tokens.add(tokenizer.nextToken());
    }
    return tokens;
}

From source file:org.ardverk.daap.DaapUtil.java

/**
 * Splits a meta String ("foo,bar,alice,bob") and stores the data in an
 * ArrayList//ww  w.j a v a2 s  .c  om
 * 
 * @param meta
 *            a meta String
 * @return the splitten meta String as ArrayList
 */
public static final List<String> parseMeta(String meta) {
    StringTokenizer tok = new StringTokenizer(meta, ",");
    List<String> list = new ArrayList<String>(tok.countTokens());
    boolean flag = false;

    while (tok.hasMoreTokens()) {
        String token = tok.nextToken();

        // Must be te fist! See DAAP documentation
        // for more info!
        if (!flag && token.equals("dmap.itemkind")) {
            list.add(0, token);
            flag = true;
        } else {
            list.add(token);
        }
    }
    return list;
}

From source file:Utilities.java

/**
 * Returns the usable area of the screen where applications can place its
 * windows.  The method subtracts from the screen the area of taskbars,
 * system menus and the like.//from   w  w w  .j av a2s  .  c o m
 *
 * @param gconf the GraphicsConfiguration of the monitor
 * @return the rectangle of the screen where one can place windows
 *
 * @since 2.5
 */
public static Rectangle getUsableScreenBounds(GraphicsConfiguration gconf) {
    if (gconf == null) {
        gconf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                .getDefaultConfiguration();
    }

    Rectangle bounds = new Rectangle(gconf.getBounds());

    String str;

    str = System.getProperty("netbeans.screen.insets"); // NOI18N

    if (str != null) {
        StringTokenizer st = new StringTokenizer(str, ", "); // NOI18N

        if (st.countTokens() == 4) {
            try {
                bounds.y = Integer.parseInt(st.nextToken());
                bounds.x = Integer.parseInt(st.nextToken());
                bounds.height -= (bounds.y + Integer.parseInt(st.nextToken()));
                bounds.width -= (bounds.x + Integer.parseInt(st.nextToken()));
            } catch (NumberFormatException ex) {
                Logger.getAnonymousLogger().log(Level.WARNING, null, ex);
            }
        }

        return bounds;
    }

    str = System.getProperty("netbeans.taskbar.height"); // NOI18N

    if (str != null) {
        bounds.height -= Integer.getInteger(str, 0).intValue();

        return bounds;
    }

    try {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Insets insets = toolkit.getScreenInsets(gconf);
        bounds.y += insets.top;
        bounds.x += insets.left;
        bounds.height -= (insets.top + insets.bottom);
        bounds.width -= (insets.left + insets.right);
    } catch (Exception ex) {
        Logger.getAnonymousLogger().log(Level.WARNING, null, ex);
    }

    return bounds;
}

From source file:org.ardverk.daap.DaapUtil.java

/**
 * This method tries the determinate the protocol version and returns it or
 * {@see #NULL} if version could not be estimated...
 *///from   w ww  . ja v a 2 s  . com
public static int getProtocolVersion(DaapRequest request) {

    if (request.isUnknownRequest())
        return DaapUtil.NULL;

    Header header = request.getHeader(DaapRequest.CLIENT_DAAP_VERSION);

    if (header == null && request.isSongRequest()) {
        header = request.getHeader(DaapRequest.USER_AGENT);
    }

    if (header == null)
        return DaapUtil.NULL;

    String name = header.getName();
    String value = header.getValue();

    // Unfortunately song requests do not have a Client-DAAP-Version
    // header. As a workaround we can estimate the protocol version
    // by User-Agent but that is weak an may break with non iTunes
    // hosts...
    if (request.isSongRequest() && name.equals(DaapRequest.USER_AGENT)) {

        // Note: the protocol version of a Song request is estimated
        // by the server with the aid of the sessionId, i.e. this block
        // is actually never touched...
        if (value.startsWith("iTunes/5.0")) {
            return DaapUtil.DAAP_VERSION_302;
        } else if (value.startsWith("iTunes/4.9") || value.startsWith("iTunes/4.8")
                || value.startsWith("iTunes/4.7") || value.startsWith("iTunes/4.6")
                || value.startsWith("iTunes/4.5")) {
            return DaapUtil.DAAP_VERSION_3;
        } else if (value.startsWith("iTunes/4.2") || value.startsWith("iTunes/4.1")) {
            return DaapUtil.DAAP_VERSION_2;
        } else if (value.startsWith("iTunes/4.0")) {
            return DaapUtil.DAAP_VERSION_1;
        } else {
            return DaapUtil.NULL;
        }
    } else {

        StringTokenizer tokenizer = new StringTokenizer(value, ".");
        int count = tokenizer.countTokens();

        if (count >= 2 && count <= 3) {
            try {

                int major = DaapUtil.NULL;
                int minor = DaapUtil.NULL;
                int patch = DaapUtil.NULL;

                major = Integer.parseInt(tokenizer.nextToken());
                minor = Integer.parseInt(tokenizer.nextToken());

                if (count == 3)
                    patch = Integer.parseInt(tokenizer.nextToken());

                return DaapUtil.toVersion(major, minor, patch);

            } catch (NumberFormatException err) {
            }
        }
    }

    return DaapUtil.NULL;
}

From source file:com.github.dozermapper.core.util.ReflectionUtils.java

public static DeepHierarchyElement[] getDeepFieldHierarchy(Class<?> parentClass, String field,
        HintContainer deepIndexHintContainer) {
    if (!MappingUtils.isDeepMapping(field)) {
        MappingUtils.throwMappingException("Field does not contain deep field delimitor");
    }/* w  w  w .  ja va 2s  . c o  m*/

    StringTokenizer toks = new StringTokenizer(field, DozerConstants.DEEP_FIELD_DELIMITER);
    Class<?> latestClass = parentClass;
    DeepHierarchyElement[] hierarchy = new DeepHierarchyElement[toks.countTokens()];
    int index = 0;
    int hintIndex = 0;
    while (toks.hasMoreTokens()) {
        String aFieldName = toks.nextToken();
        String theFieldName = aFieldName;
        int collectionIndex = -1;

        if (aFieldName.contains("[")) {
            theFieldName = aFieldName.substring(0, aFieldName.indexOf("["));
            collectionIndex = Integer
                    .parseInt(aFieldName.substring(aFieldName.indexOf("[") + 1, aFieldName.indexOf("]")));
        }

        PropertyDescriptor propDescriptor = findPropertyDescriptor(latestClass, theFieldName,
                deepIndexHintContainer);
        DeepHierarchyElement r = new DeepHierarchyElement(propDescriptor, collectionIndex);

        if (propDescriptor == null) {
            MappingUtils
                    .throwMappingException("Exception occurred determining deep field hierarchy for Class --> "
                            + parentClass.getName() + ", Field --> " + field
                            + ".  Unable to determine property descriptor for Class --> "
                            + latestClass.getName() + ", Field Name: " + aFieldName);
        }

        latestClass = propDescriptor.getPropertyType();
        if (toks.hasMoreTokens()) {
            if (latestClass.isArray()) {
                latestClass = latestClass.getComponentType();
            } else if (Collection.class.isAssignableFrom(latestClass)) {
                Class<?> genericType = determineGenericsType(parentClass.getClass(), propDescriptor);

                if (genericType == null && deepIndexHintContainer == null) {
                    MappingUtils.throwMappingException(
                            "Hint(s) or Generics not specified.  Hint(s) or Generics must be specified for deep mapping with indexed field(s). "
                                    + "Exception occurred determining deep field hierarchy for Class --> "
                                    + parentClass.getName() + ", Field --> " + field
                                    + ".  Unable to determine property descriptor for Class --> "
                                    + latestClass.getName() + ", Field Name: " + aFieldName);
                }

                if (genericType != null) {
                    latestClass = genericType;
                } else {
                    latestClass = deepIndexHintContainer.getHint(hintIndex);
                    hintIndex += 1;
                }
            }
        }
        hierarchy[index++] = r;
    }

    return hierarchy;
}

From source file:com.sfs.DataFilter.java

/**
 * Parses the text data./*from   w ww  .  jav  a 2  s.  co m*/
 *
 * @param text the text
 *
 * @return the tree map< integer, tree map< integer, string>>
 */
public static TreeMap<Integer, TreeMap<Integer, String>> parseTextData(final String text) {

    TreeMap<Integer, TreeMap<Integer, String>> parsedData = new TreeMap<Integer, TreeMap<Integer, String>>();

    // This counter holds the maximum number of columns provided
    int maxNumberOfTokens = 0;

    if (text != null) {
        StringTokenizer tokenizer = new StringTokenizer(text, "\n");

        int lineCounter = 1;

        while (tokenizer.hasMoreTokens()) {
            String line = tokenizer.nextToken();
            TreeMap<Integer, String> parsedLine = new TreeMap<Integer, String>();

            final StringTokenizer tabTokenizer = new StringTokenizer(line, "\t");
            if (tabTokenizer.countTokens() > 1) {
                parsedLine = tokenizerToMap(tabTokenizer);
            } else {
                final StringTokenizer commaTokenizer = new StringTokenizer(line, ",");
                parsedLine = tokenizerToMap(commaTokenizer);
            }
            if (parsedLine.size() > maxNumberOfTokens) {
                maxNumberOfTokens = parsedLine.size();
            }

            parsedData.put(lineCounter, parsedLine);
            lineCounter++;
        }
    }

    // Now cycle through all the parsed data
    // Ensure that each row has the same (max) number of tokens
    for (int rowIndex : parsedData.keySet()) {
        TreeMap<Integer, String> parsedLine = parsedData.get(rowIndex);

        // This map holds the final values
        TreeMap<Integer, String> columnTokens = new TreeMap<Integer, String>();

        for (int i = 0; i < maxNumberOfTokens; i++) {
            int columnIndex = i + 1;
            if (parsedLine.containsKey(columnIndex)) {
                String value = parsedLine.get(columnIndex);
                columnTokens.put(columnIndex, value);
            } else {
                columnTokens.put(columnIndex, "");
            }
        }
        parsedData.put(rowIndex, columnTokens);
    }

    return parsedData;
}

From source file:org.apache.axis2.jaxws.description.impl.DescriptionUtils.java

static String makeNamespaceFromPackageName(String packageName, String protocol) {
    if (DescriptionUtils.isEmpty(protocol)) {
        protocol = "http";
    }// ww  w  .  jav  a2s .c  o m
    if (DescriptionUtils.isEmpty(packageName)) {
        return protocol + "://" + NO_PACKAGE_HOST_NAME;
    }
    StringTokenizer st = new StringTokenizer(packageName, ".");
    String[] words = new String[st.countTokens()];
    for (int i = 0; i < words.length; ++i)
        words[i] = st.nextToken();

    StringBuffer sb = new StringBuffer(80);
    for (int i = words.length - 1; i >= 0; --i) {
        String word = words[i];
        // seperate with dot
        if (i != words.length - 1)
            sb.append('.');
        sb.append(word);
    }
    return protocol + "://" + sb.toString() + "/";
}

From source file:edu.stanford.muse.index.NER.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));
        }//w  ww  .  jav a  2s  .c  o m
    }
    lnr.close();
}

From source file:net.wastl.webmail.misc.Helper.java

public static String decryptTEA(String src) {
    StringTokenizer tok = new StringTokenizer(src, ": ");
    byte[] key = new BigInteger(str_key, 16).toByteArray();
    TEA tea = new TEA(key);
    byte inb[] = new byte[tok.countTokens()];
    for (int i = 0; i < inb.length && tok.hasMoreTokens(); i++) {
        String s = tok.nextToken();
        try {/*from ww  w . j a v a  2 s  .c om*/
            inb[i] = (byte) (Integer.parseInt(s, 16) - 128);
        } catch (NumberFormatException ex) {
            throw new NumberFormatException("Could not convert string '" + s + "' to byte.");
        }
    }
    byte dec[] = tea.decode(inb, inb.length);
    String s = new String(dec);
    // log.debug("encryptTEA: Returning "+s);
    java.util.regex.Matcher m = nonGraphPattern.matcher(s);
    if (!m.find())
        return s;
    log.warn("Executed work-around for TEA decryption bug");
    String fixedS = m.replaceFirst("");
    if (s.equals(fixedS))
        log.warn("The TEA work-around was not needed in this case");
    // I believe the rot of the bug is with padding in the TEA.decode()
    // method(s).  I do not have time to fix the real problem there.
    return fixedS;
}