Example usage for java.util StringTokenizer hasMoreTokens

List of usage examples for java.util StringTokenizer hasMoreTokens

Introduction

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

Prototype

public boolean hasMoreTokens() 

Source Link

Document

Tests if there are more tokens available from this tokenizer's string.

Usage

From source file:com.bdaum.zoom.net.core.ftp.FtpAccount.java

/**
 * Finds an FTP account in the systems preference settings
 *
 * @param name//w  w  w .  j a va  2 s .  c om
 *            - account name
 * @return account object
 */
public static FtpAccount findAccount(String name) {
    String ess = Platform.getPreferencesService().getString(Activator.PLUGIN_ID,
            PreferenceConstants.FTPACCOUNTS, "", null); //$NON-NLS-1$
    String search = name + FIELDSEP;
    StringTokenizer st = new StringTokenizer(ess, SEPS);
    while (st.hasMoreTokens()) {
        String s = st.nextToken();
        if (s.startsWith(search))
            return new FtpAccount(s);
    }
    return null;
}

From source file:it.unibo.alchemist.language.EnvironmentBuilder.java

@SuppressWarnings("unchecked")
private static Object parseAndCreate(final Class<?> clazz, final String val, final Map<String, Object> env,
        final RandomGenerator random)
        throws InstantiationException, IllegalAccessException, InvocationTargetException {
    if (clazz.isAssignableFrom(RandomGenerator.class) && val.equalsIgnoreCase("random")) {
        L.debug("Random detected! Class " + clazz.getSimpleName() + ", param: " + val);
        if (random == null) {
            L.error("Random instatiation required, but RandomGenerator not yet defined.");
        }/*from   ww w. j  a va 2 s . co m*/
        return random;
    }
    if (clazz.isPrimitive() || PrimitiveUtils.classIsWrapper(clazz)) {
        L.debug(val + " is a primitive or a wrapper: " + clazz);
        if ((clazz.isAssignableFrom(Boolean.TYPE) || clazz.isAssignableFrom(Boolean.class))
                && (val.equalsIgnoreCase("true") || val.equalsIgnoreCase("false"))) {
            return Boolean.parseBoolean(val);
        }
        /*
         * If Number is in clazz's hierarchy
         */
        if (PrimitiveUtils.classIsNumber(clazz)) {
            final Optional<Number> num = extractNumber(val);
            if (num.isPresent()) {
                final Optional<Number> castNum = PrimitiveUtils.castIfNeeded(clazz, num.get());
                /*
                 * If method requires Object or unsupported Number, return
                 * what was parsed.
                 */
                return castNum.orElse(num.get());
            }
        }
        if (Character.TYPE.equals(clazz) || Character.class.equals(clazz)) {
            return val.charAt(0);
        }
    }
    if (List.class.isAssignableFrom(clazz) && val.startsWith("[") && val.endsWith("]")) {
        final List<Constructor<List<?>>> l = unsafeExtractConstructors(clazz);
        @SuppressWarnings("rawtypes")
        final List list = tryToBuild(l, new ArrayList<String>(0), env, random);
        final StringTokenizer strt = new StringTokenizer(val.substring(1, val.length() - 1), ",; ");
        while (strt.hasMoreTokens()) {
            final String sub = strt.nextToken();
            final Object o = tryToParse(sub, env, random);
            if (o == null) {
                L.debug("WARNING: list elemnt skipped: " + sub);
            } else {
                list.add(o);
            }
        }
        return list;
    }
    L.debug(val + " is not a primitive: " + clazz + ". Searching it in the environment...");
    final Object o = env.get(val);
    if (o != null && clazz.isInstance(o)) {
        return o;
    }
    if (Time.class.isAssignableFrom(clazz)) {
        return new DoubleTime(Double.parseDouble(val));
    }
    if (clazz.isAssignableFrom(String.class)) {
        L.debug("String detected! Passing " + val + " back.");
        return val;
    }
    L.debug(val + " not found or class not compatible, unable to go further.");
    return null;
}

From source file:com.bdaum.zoom.net.core.ftp.FtpAccount.java

/**
 * Obtains all FTP accounts defined in the systems preference settings
 *
 * @return list of account objects/*w  w w  .  ja va  2  s.  com*/
 */
public static List<FtpAccount> getAllAccounts() {
    ArrayList<FtpAccount> ftpAccounts = new ArrayList<FtpAccount>();
    String s = Platform.getPreferencesService().getString(Activator.PLUGIN_ID, PreferenceConstants.FTPACCOUNTS,
            "", null); //$NON-NLS-1$
    if (s != null) {
        StringTokenizer st = new StringTokenizer(s, SEPS);
        while (st.hasMoreTokens())
            ftpAccounts.add(new FtpAccount(st.nextToken()));
    }
    return ftpAccounts;
}

From source file:com.adito.core.RequestParameterMap.java

private static void parseQuery(MultiMap map, String query) {
    StringTokenizer t = new StringTokenizer(query, "&");
    while (t.hasMoreTokens()) {
        String parm = t.nextToken();
        int pidx = parm.indexOf('=');
        String name = pidx == -1 ? parm : parm.substring(0, pidx);
        String value = pidx == -1 ? "" : parm.substring(pidx + 1);
        map.add(name, value);//from   w  w w  . j  av a 2s  .co  m
    }
}

From source file:it.univpm.deit.semedia.musicuri.core.Toolset.java

/**
 * Gets the first occurence of an integer within a String. It is used to 
 * retrieve the integer test case identifier from filenames that are used for 
 * testing and evaluation purposes in this project
 * @param filename the filename to remove the identifier from
 * @return a string containing the filename without an identifier
 *///from  w w w . ja  v  a 2s.  com
public static int getTestCaseIdentifier(String filename) {
    //TODO: currently returns any integer that can be cast. should return the first token
    StringTokenizer st = new StringTokenizer(filename, " ,._-~\"'`/()[]:{}+#\t\n\r");
    int returnIdentifier = -1;

    // loop over all tokens in string
    while (st.hasMoreTokens()) {
        // get the next
        String token = st.nextToken();
        // if it has 4 characters
        if (token.length() == 4) {
            // try to cast it to an integer
            try {
                // if it can be cast to an integer, this is the test-case
                // identifier, assuming that no 4-digit long integer exists
                // in the names of any of the test-case mp3's parent folders
                returnIdentifier = Integer.parseInt(token);
            } catch (NumberFormatException e) {
                // if it can't, do nothing
                System.out.print("");
            }
        }
    }

    return returnIdentifier;
}

From source file:com.kamuda.common.exception.ExceptionUtils.java

/**
 * <p>Produces a <code>List</code> of stack frames - the message
 * is not included.</p>/*from w  ww .j  a  v a  2s. c om*/
 *
 * <p>This works in most cases - it will only fail if the exception
 * message contains a line that starts with:
 * <code>&quot;&nbsp;&nbsp;&nbsp;at&quot;.</code></p>
 * 
 * @param t is any throwable
 * @return List of stack frames
 */
static List getStackFrameList(Throwable t) {
    String stackTrace = getStackTrace(t);
    String linebreak = LINE_SEPARATOR;
    StringTokenizer frames = new StringTokenizer(stackTrace, linebreak);
    List list = new LinkedList();
    boolean traceStarted = false;
    while (frames.hasMoreTokens()) {
        String token = frames.nextToken();
        // Determine if the line starts with <whitespace>at
        int at = token.indexOf("at");
        if (at != -1 && token.substring(0, at).trim().length() == 0) {
            traceStarted = true;
            list.add(token);
        } else if (traceStarted) {
            break;
        }
    }
    return list;
}

From source file:org.toobsframework.data.beanutil.BeanMonkey.java

public static void concatPropertyList(Object bean, String propertyList, StringBuffer result, boolean quoted)
        throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    if (bean == null || propertyList == null || result == null) {
        return;/*from w  w w.  j  a  v a 2 s. co  m*/
    }

    StringTokenizer st = new StringTokenizer(propertyList, ",");
    while (st.hasMoreTokens()) {
        String name = st.nextToken();
        Object value = PropertyUtils.getProperty(bean, name);
        if (value instanceof String && value != null) {
            result.append(" ");
            if (quoted)
                result.append("\"");
            result.append(value);
            if (quoted)
                result.append("\"");
        } else if (value instanceof Collection && value != null) {
            Iterator valIter = ((Collection) value).iterator();
            while (valIter.hasNext()) {
                concatPropertyList(valIter.next(), "displayName", result, quoted);
                // result.append(" " + ConvertUtils.convert(valIter.next()));
            }
        } else if (value != null) {
            result.append(" ");
            if (quoted)
                result.append("\"");
            result.append(ConvertUtils.convert(value));
            if (quoted)
                result.append("\"");
        }
    }
}

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

private static String nextLocation(final StringTokenizer st) {
    String retVal = null;//from   ww  w  .jav a  2  s  .c o  m

    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:com.amalto.core.storage.StorageMetadataUtils.java

private static void _path(ComplexTypeMetadata type, FieldMetadata target, Stack<FieldMetadata> path,
        Set<ComplexTypeMetadata> processedTypes, boolean includeReferences) {
    // Various optimizations for very simple cases
    if (type == null) {
        throw new IllegalArgumentException("Origin can not be null");
    }/*from   w w  w . j av  a 2  s . co  m*/
    if (target == null) {
        throw new IllegalArgumentException("Target field can not be null");
    }
    if (Storage.PROJECTION_TYPE.equals(type.getName()) && type.hasField(target.getName())) {
        path.push(type.getField(target.getName()));
    }
    if (target.getContainingType() instanceof ContainedComplexTypeMetadata) {
        String targetPath = target.getPath();
        if (type.hasField(targetPath)) {
            StringTokenizer tokenizer = new StringTokenizer(targetPath, "/"); //$NON-NLS-1$
            StringBuilder currentPath = new StringBuilder();
            while (tokenizer.hasMoreTokens()) {
                currentPath.append(tokenizer.nextToken()).append('/');
                path.add(type.getField(currentPath.toString()));
            }
            return;
        }
    }
    //
    if (processedTypes.contains(type)) {
        return;
    }
    processedTypes.add(type);
    Collection<FieldMetadata> fields = type.getFields();
    for (FieldMetadata current : fields) {
        path.push(current);
        if (current.equals(target)) {
            return;
        }
        if (current instanceof ContainedTypeFieldMetadata) {
            ComplexTypeMetadata containedType = ((ContainedTypeFieldMetadata) current).getContainedType();
            _path(containedType, target, path, processedTypes, includeReferences);
            if (path.peek().equals(target)) {
                return;
            }
            for (ComplexTypeMetadata subType : containedType.getSubTypes()) {
                for (FieldMetadata field : subType.getFields()) {
                    if (field.getDeclaringType() == subType) {
                        _path(subType, target, path, processedTypes, includeReferences);
                        if (path.peek().equals(target)) {
                            return;
                        }
                    }
                }
            }
        } else if (current instanceof ReferenceFieldMetadata) {
            if (includeReferences) {
                ComplexTypeMetadata referencedType = ((ReferenceFieldMetadata) current).getReferencedType();
                _path(referencedType, target, path, processedTypes, true);
                if (path.peek().equals(target)) {
                    return;
                }
                for (ComplexTypeMetadata subType : referencedType.getSubTypes()) {
                    for (FieldMetadata field : subType.getFields()) {
                        if (field.getDeclaringType() == subType) {
                            _path(subType, target, path, processedTypes, true);
                            if (path.peek().equals(target)) {
                                return;
                            }
                        }
                    }
                }
            }
        }
        path.pop();
    }
}

From source file:com.isecpartners.gizmo.FourthIdea.java

private static String[] translateCommandline(final String toProcess) {
    if (toProcess == null || toProcess.length() == 0) {
        // no command? no string
        return new String[0];
    }/*w  ww .j a v  a2  s  .co  m*/

    // parse with a simple finite state machine

    final int normal = 0;
    final int inQuote = 1;
    final int inDoubleQuote = 2;
    int state = normal;
    StringTokenizer tok = new StringTokenizer(toProcess, "\"\' ", true);
    Vector v = new Vector();
    StringBuffer current = new StringBuffer();
    boolean lastTokenHasBeenQuoted = false;

    while (tok.hasMoreTokens()) {
        String nextTok = tok.nextToken();
        switch (state) {
        case inQuote:
            if ("\'".equals(nextTok)) {
                lastTokenHasBeenQuoted = true;
                state = normal;
            } else {
                current.append(nextTok);
            }
            break;
        case inDoubleQuote:
            if ("\"".equals(nextTok)) {
                lastTokenHasBeenQuoted = true;
                state = normal;
            } else {
                current.append(nextTok);
            }
            break;
        default:
            if ("\'".equals(nextTok)) {
                state = inQuote;
            } else if ("\"".equals(nextTok)) {
                state = inDoubleQuote;
            } else if (" ".equals(nextTok)) {
                if (lastTokenHasBeenQuoted || current.length() != 0) {
                    v.addElement(current.toString());
                    current = new StringBuffer();
                }
            } else {
                current.append(nextTok);
            }
            lastTokenHasBeenQuoted = false;
            break;
        }
    }

    if (lastTokenHasBeenQuoted || current.length() != 0) {
        v.addElement(current.toString());
    }

    if (state == inQuote || state == inDoubleQuote) {
        throw new IllegalArgumentException("Unbalanced quotes in " + toProcess);
    }

    String[] args = new String[v.size()];
    v.copyInto(args);
    return args;
}