Search in sources :

Example 31 with StringTokenizer

use of java.util.StringTokenizer in project grails-core by grails.

the class CommandLineParser method translateCommandline.

/**
     * Crack a command line.
     * @param toProcess the command line to process.
     * @return the command line broken into strings.
     * An empty or null toProcess parameter results in a zero sized array.
     */
public static String[] translateCommandline(String toProcess) {
    if (toProcess == null || toProcess.length() == 0) {
        //no command? no string
        return new String[0];
    }
    // parse with a simple finite state machine
    final int normal = 0;
    final int inQuote = 1;
    final int inDoubleQuote = 2;
    int state = normal;
    final StringTokenizer tok = new StringTokenizer(toProcess, "\"\' ", true);
    final ArrayList<String> result = new ArrayList<String>();
    final StringBuilder current = new StringBuilder();
    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) {
                        result.add(current.toString());
                        current.setLength(0);
                    }
                } else {
                    current.append(nextTok);
                }
                lastTokenHasBeenQuoted = false;
                break;
        }
    }
    if (lastTokenHasBeenQuoted || current.length() != 0) {
        result.add(current.toString());
    }
    if (state == inQuote || state == inDoubleQuote) {
        throw new ParseException("unbalanced quotes in " + toProcess);
    }
    return result.toArray(new String[result.size()]);
}
Also used : StringTokenizer(java.util.StringTokenizer) ArrayList(java.util.ArrayList)

Example 32 with StringTokenizer

use of java.util.StringTokenizer in project hazelcast by hazelcast.

the class GetCommandParser method parser.

@Override
public TextCommand parser(TextReadHandler readHandler, String cmd, int space) {
    String key = cmd.substring(space + 1);
    if (key.indexOf(' ') == -1) {
        GetCommand r = new GetCommand(key);
        readHandler.publishRequest(r);
    } else {
        StringTokenizer st = new StringTokenizer(key);
        List<String> keys = new ArrayList<String>();
        while (st.hasMoreTokens()) {
            String singleKey = st.nextToken();
            keys.add(singleKey);
        }
        readHandler.publishRequest(new BulkGetCommand(keys));
    }
    return null;
}
Also used : StringTokenizer(java.util.StringTokenizer) ArrayList(java.util.ArrayList)

Example 33 with StringTokenizer

use of java.util.StringTokenizer in project hazelcast by hazelcast.

the class IncrementCommandParser method parser.

@Override
public TextCommand parser(TextReadHandler readHandler, String cmd, int space) {
    StringTokenizer st = new StringTokenizer(cmd);
    st.nextToken();
    String key;
    int value;
    boolean noReply = false;
    if (st.hasMoreTokens()) {
        key = st.nextToken();
    } else {
        return new ErrorCommand(ERROR_CLIENT);
    }
    if (st.hasMoreTokens()) {
        value = Integer.parseInt(st.nextToken());
    } else {
        return new ErrorCommand(ERROR_CLIENT);
    }
    if (st.hasMoreTokens()) {
        noReply = "noreply".equals(st.nextToken());
    }
    return new IncrementCommand(type, key, value, noReply);
}
Also used : StringTokenizer(java.util.StringTokenizer)

Example 34 with StringTokenizer

use of java.util.StringTokenizer in project hazelcast by hazelcast.

the class HttpDeleteCommandParser method parser.

@Override
public TextCommand parser(TextReadHandler readHandler, String cmd, int space) {
    StringTokenizer st = new StringTokenizer(cmd);
    st.nextToken();
    String uri;
    if (st.hasMoreTokens()) {
        uri = st.nextToken();
    } else {
        return new ErrorCommand(ERROR_CLIENT);
    }
    return new HttpDeleteCommand(uri);
}
Also used : StringTokenizer(java.util.StringTokenizer) ErrorCommand(com.hazelcast.internal.ascii.memcache.ErrorCommand)

Example 35 with StringTokenizer

use of java.util.StringTokenizer in project hazelcast by hazelcast.

the class HttpGetCommandParser method parser.

@Override
public TextCommand parser(TextReadHandler readHandler, String cmd, int space) {
    StringTokenizer st = new StringTokenizer(cmd);
    st.nextToken();
    String uri;
    if (st.hasMoreTokens()) {
        uri = st.nextToken();
    } else {
        return new ErrorCommand(ERROR_CLIENT);
    }
    return new HttpGetCommand(uri);
}
Also used : StringTokenizer(java.util.StringTokenizer) ErrorCommand(com.hazelcast.internal.ascii.memcache.ErrorCommand)

Aggregations

StringTokenizer (java.util.StringTokenizer)2535 ArrayList (java.util.ArrayList)521 IOException (java.io.IOException)260 HashMap (java.util.HashMap)160 HashSet (java.util.HashSet)150 File (java.io.File)136 BufferedReader (java.io.BufferedReader)112 Map (java.util.Map)103 Set (java.util.Set)91 Iterator (java.util.Iterator)84 List (java.util.List)78 ParseResult (pcgen.rules.persistence.token.ParseResult)68 InputStreamReader (java.io.InputStreamReader)61 URL (java.net.URL)60 Vector (java.util.Vector)53 FileReader (java.io.FileReader)51 NoSuchElementException (java.util.NoSuchElementException)49 MalformedURLException (java.net.MalformedURLException)42 FileInputStream (java.io.FileInputStream)41 CDOMReference (pcgen.cdom.base.CDOMReference)40