Example usage for java.io StreamTokenizer TT_WORD

List of usage examples for java.io StreamTokenizer TT_WORD

Introduction

In this page you can find the example usage for java.io StreamTokenizer TT_WORD.

Prototype

int TT_WORD

To view the source code for java.io StreamTokenizer TT_WORD.

Click Source Link

Document

A constant indicating that a word token has been read.

Usage

From source file:eu.scape_project.pt.util.PipedArgsParser.java

private Entry PAIR() throws IOException {
    String key = null;//  w  w w .j  av a 2 s. co m
    String value = null;

    if (nextToken() != '-')
        throw new IOException("unrecognized token " + (tokenizer.ttype >= 32 ? (char) tokenizer.ttype : ""));

    if (nextToken() != StreamTokenizer.TT_WORD)
        throw new IOException("unrecognized token, expecting key word");
    key = tokenizer.sval;

    if (nextToken() != '=')
        throw new IOException("unrecognized token, expecting '='");
    LOG.debug("= found");

    if (nextToken() != '"' && currentToken() != StreamTokenizer.TT_WORD)
        throw new IOException("unrecognized token, expecting string literal");
    LOG.debug("\" found");
    LOG.debug("value found");

    value = tokenizer.sval;

    return new SimpleEntry<String, String>(key, value);
}

From source file:eu.scape_project.pt.util.PipedArgsParser.java

public Command COMMAND() throws IOException {
    Command c = new Command();
    if (nextToken() != StreamTokenizer.TT_WORD)
        throw new IOException("command must start with tool literal");
    c.tool = tokenizer.sval;/*ww w  .  j ava 2 s  . c  o  m*/
    if (nextToken() != StreamTokenizer.TT_WORD)
        throw new IOException("second argument of command must be an action literal");
    c.action = tokenizer.sval;
    while (nextToken() == '-') {
        Entry<String, String> pair = PAIR();
        c.pairs.put(pair.getKey(), pair.getValue());
    }
    return c;
}

From source file:com.fluffypeople.managesieve.ManageSieveClient.java

/**
 * Authenticate against the remote server using SASL.
 *
 * The CallbackHandler should be setup appropriately, for example:
 * <pre>/*w  w  w. j  av a 2 s .c  o  m*/
 * <code>
 *
 * CallbackHandler cbh = new CallbackHandler() {
 *     public void handle(Callback[] clbcks) throws IOException,  UnsupportedCallbackException {
 *         for (Callback cb : clbcks) {
 *             if (cb instanceof NameCallback) {
 *                 NameCallback name = (NameCallback) cb;
 *                 name.setName("user");
 *             } else if (cb instanceof PasswordCallback) {
 *                 PasswordCallback passwd = (PasswordCallback) cb;
 *                 passwd.setPassword("secret".toCharArray());
 *             }
 *         }
 *     }
 * };
 * </code>
 * </pre>
 *
 * @param cbh CallbackHandler[] list of call backs that will be called by
 * the SASL code
 * @return ManageSieveResponse from the server, OK is authenticated, NO
 * means a problem
 * @throws SaslException
 * @throws IOException
 * @throws ParseException
 */
public synchronized ManageSieveResponse authenticate(final CallbackHandler cbh)
        throws SaslException, IOException, ParseException {

    SaslClient sc = Sasl.createSaslClient(cap.getSASLMethods(), null, "sieve", hostname, null, cbh);

    String mechanism = escapeString(sc.getMechanismName());
    if (sc.hasInitialResponse()) {
        byte[] ir = sc.evaluateChallenge(new byte[0]);
        String ready = new String(Base64.encodeBase64(ir));
        ready = encodeString(ready.trim());
        sendCommand("AUTHENTICATE", mechanism, ready);
    } else {
        sendCommand("AUTHENTICATE", mechanism);
    }

    int token;
    ManageSieveResponse resp = null;
    do {
        token = in.nextToken();
        if (token == DQUOTE) {
            // String - so more data for the auth sequence
            in.pushBack();
            String msg = parseString();
            byte[] response = sc.evaluateChallenge(msg.getBytes());
            sendLine(encodeString(new String(response)));
        } else if (token == StreamTokenizer.TT_WORD) {
            in.pushBack();
            resp = parseResponse();
            break;
        } else {
            throw new ParseException(
                    "Expecting DQUOTE/WORD, got " + tokenToString(token) + " at line " + in.lineno());
        }
    } while (!sc.isComplete());

    // Complete
    sc.dispose();
    return resp;
}

From source file:edu.buffalo.cse.pigout.parser.PigOutMacro.java

private static boolean isMultiValueReturn(StreamTokenizer st, List<String> mlist, boolean comma)
        throws IOException {
    int lookahead = st.nextToken();
    if ((comma && lookahead == StreamTokenizer.TT_WORD) || (!comma && matchChar(st, ',', false))) {
        if (matchDollarAlias(st, false)) {
            mlist.add(st.sval);// ww w.ja v a  2  s.c o m
        }
        return isMultiValueReturn(st, mlist, !comma);
    }
    if (!comma && lookahead == '=' && !matchChar(st, '=', true)) {
        return true;
    }
    return false;
}

From source file:edu.buffalo.cse.pigout.parser.PigOutMacro.java

private static boolean matchDollarAlias(StreamTokenizer st, boolean next) throws IOException {
    int type = next ? st.nextToken() : st.ttype;
    if (type == StreamTokenizer.TT_WORD && st.sval.charAt(0) == '$' && st.sval.length() > 1) {
        return true;
    }/*from w  ww . ja v a  2 s  .c o  m*/
    if (next)
        st.pushBack();
    return false;
}

From source file:edu.buffalo.cse.pigout.parser.PigOutMacro.java

private static boolean matchWord(StreamTokenizer st, String word, boolean next) throws IOException {
    int type = next ? st.nextToken() : st.ttype;
    if (type == StreamTokenizer.TT_WORD && st.sval.equalsIgnoreCase(word)) {
        return true;
    }//  w  ww .ja v  a  2 s  .  c o  m
    if (next)
        st.pushBack();

    return false;
}

From source file:com.fluffypeople.managesieve.ManageSieveClient.java

/**
 * "This command lists the scripts the user has on the server". The results
 * are stored into the @code{List<SieveScript>} passed in. Any existing
 * contents of this list will be lost. Up to one of the scripts listed will
 * be marked active./*from w ww.ja va2  s.c  o  m*/
 *
 * @param scripts @code{List<SieveScript>} non-null List of scripts. Will be
 * cleared if not zero length, even if there is a problem
 * @return ManageSieveResponse OK - list was fetched, NO - there was a
 * problem.
 * @throws IOException
 * @throws ParseException
 */
public synchronized ManageSieveResponse listscripts(List<SieveScript> scripts)
        throws IOException, ParseException {
    if (!scripts.isEmpty()) {
        scripts.clear();
    }
    sendCommand("LISTSCRIPTS");
    while (true) {
        int token = in.nextToken();
        switch (token) {
        case DQUOTE:
        case LEFT_CURRLY_BRACE:
            in.pushBack();

            String scriptName = parseString();
            boolean isActive = false;
            token = in.nextToken();
            if (token == StreamTokenizer.TT_WORD) {
                if (in.sval.equals("ACTIVE")) {
                    // active script;
                    isActive = true;
                } else {
                    throw new ParseException("Unexpected word " + in.sval + " at line " + in.lineno());
                }
                token = in.nextToken();
            }

            if (token == StreamTokenizer.TT_EOL) {
                scripts.add(new SieveScript(scriptName, null, isActive));
            } else {
                throw new ParseException(
                        "Expected EOL, got  " + tokenToString(token) + " at line " + in.lineno());
            }
            break;
        case StreamTokenizer.TT_WORD:
            in.pushBack();
            return parseResponse();
        default:
            throw new ParseException("Unexpected token " + tokenToString(token) + " at line " + in.lineno());
        }
    }
}

From source file:com.redskyit.scriptDriver.RunTests.java

private File runScript(String filename) throws Exception {
    File file = new File(filename);
    StreamTokenizer tokenizer = openScript(file);
    while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
        if (tokenizer.ttype == StreamTokenizer.TT_WORD) {
            runCommand(tokenizer, file, file.getName(), new ExecutionContext());
        }//from   w  ww.java 2s.  co  m
    }
    return file;
}

From source file:com.ecyrd.jspwiki.plugin.PluginManager.java

/**
 *  Parses plugin arguments.  Handles quotes and all other kewl stuff.
 *
 *  <h3>Special parameters</h3>
 *  The plugin body is put into a special parameter defined by {@link #PARAM_BODY};
 *  the plugin's command line into a parameter defined by {@link #PARAM_CMDLINE};
 *  and the bounds of the plugin within the wiki page text by a parameter defined
 *  by {@link #PARAM_BOUNDS}, whose value is stored as a two-element int[] array,
 *  i.e., <tt>[start,end]</tt>.
 *
 * @param argstring The argument string to the plugin.  This is
 *  typically a list of key-value pairs, using "'" to escape
 *  spaces in strings, followed by an empty line and then the
 *  plugin body.  In case the parameter is null, will return an
 *  empty parameter list.//from  w  w  w  . ja  v a  2s  .c om
 *
 * @return A parsed list of parameters.
 *
 * @throws IOException If the parsing fails.
 */
public Map parseArgs(String argstring) throws IOException {
    HashMap<String, Object> arglist = new HashMap<String, Object>();

    //
    //  Protection against funny users.
    //
    if (argstring == null)
        return arglist;

    arglist.put(PARAM_CMDLINE, argstring);

    StringReader in = new StringReader(argstring);
    StreamTokenizer tok = new StreamTokenizer(in);
    int type;

    String param = null;
    String value = null;

    tok.eolIsSignificant(true);

    boolean potentialEmptyLine = false;
    boolean quit = false;

    while (!quit) {
        String s;

        type = tok.nextToken();

        switch (type) {
        case StreamTokenizer.TT_EOF:
            quit = true;
            s = null;
            break;

        case StreamTokenizer.TT_WORD:
            s = tok.sval;
            potentialEmptyLine = false;
            break;

        case StreamTokenizer.TT_EOL:
            quit = potentialEmptyLine;
            potentialEmptyLine = true;
            s = null;
            break;

        case StreamTokenizer.TT_NUMBER:
            s = Integer.toString((int) tok.nval);
            potentialEmptyLine = false;
            break;

        case '\'':
            s = tok.sval;
            break;

        default:
            s = null;
        }

        //
        //  Assume that alternate words on the line are
        //  parameter and value, respectively.
        //
        if (s != null) {
            if (param == null) {
                param = s;
            } else {
                value = s;

                arglist.put(param, value);

                // log.debug("ARG: "+param+"="+value);
                param = null;
            }
        }
    }

    //
    //  Now, we'll check the body.
    //

    if (potentialEmptyLine) {
        StringWriter out = new StringWriter();
        FileUtil.copyContents(in, out);

        String bodyContent = out.toString();

        if (bodyContent != null) {
            arglist.put(PARAM_BODY, bodyContent);
        }
    }

    return arglist;
}

From source file:com.redskyit.scriptDriver.RunTests.java

private boolean executeFunction(String name, File file, StreamTokenizer parent, ExecutionContext script)
        throws Exception {
    ExecutionContext context = functions.get(name);
    if (null != context) {
        // If this context has parameters then gather argument values
        if (null != parent && null != script) {
            List<String> params = context.getParams();
            if (null != params && params.size() > 0) {
                context.clearArgs();//  w  w w  .j  a v  a 2  s .  c  om
                int count = params.size();
                while (count-- > 0) {
                    // get argument
                    parent.nextToken();
                    System.out.print(' ');
                    switch (parent.ttype) {
                    case StreamTokenizer.TT_NUMBER:
                        System.out.print(parent.nval);
                        context.addArg(parent.nval);
                        break;
                    default:
                        System.out.print(parent.sval);
                        context.addArg(script.getExpandedString(parent));
                        break;
                    }
                }
            }
            System.out.println();
        }

        // Get function body and execute it
        String code = context.getBody();
        if (null != code) {
            StreamTokenizer tokenizer = openString(code);
            while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
                if (tokenizer.ttype == StreamTokenizer.TT_WORD) {
                    runCommand(tokenizer, file, name, context);
                }
            }
            return true;
        }
    }
    return false;
}