Java Code Examples for java.io.StreamTokenizer#nextToken()

The following examples show how to use java.io.StreamTokenizer#nextToken() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Harness.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
Benchmark parseBenchClass(StreamTokenizer tokens)
    throws IOException, ConfigFormatException
{
    Benchmark bench;
    switch (tokens.ttype) {
        case StreamTokenizer.TT_WORD:
        case '"':
            try {
                Class cls = Class.forName(tokens.sval);
                bench = (Benchmark) cls.newInstance();
            } catch (Exception e) {
                throw new ConfigFormatException("unable to instantiate " +
                        "benchmark \"" + tokens.sval + "\" on line " +
                        tokens.lineno());
            }
            tokens.nextToken();
            return bench;

        default:
            throw new ConfigFormatException("missing benchmark class " +
                    "name on line " + tokens.lineno());
    }
}
 
Example 2
Source File: CodeValidator.java    From CodeDefenders with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static List<String> getTokens(StreamTokenizer st) {
    final List<String> tokens = new LinkedList<>();
    try {
        while (st.nextToken() != StreamTokenizer.TT_EOF) {
            if (st.ttype == StreamTokenizer.TT_NUMBER) {
                tokens.add(String.valueOf(st.nval));
            } else if (st.ttype == StreamTokenizer.TT_WORD) {
                tokens.add(st.sval.trim());
            } else if (st.sval != null) {
                if (((char) st.ttype) == '"' || ((char) st.ttype) == '\'') {
                    tokens.add('"' + st.sval + '"');
                } else {
                    tokens.add(st.sval.trim());
                }
            } else if ((char) st.ttype != ' ') {
                tokens.add(Character.toString((char) st.ttype));
            }
        }
    } catch (IOException e) {
        logger.warn("Swallowing IOException", e); // TODO: Why are we swallowing this?
    }
    return tokens;
}
 
Example 3
Source File: BungeeChatCommand.java    From BungeeChat2 with GNU General Public License v3.0 6 votes vote down vote up
private String getUnquotedString(String str) {
  if ((str == null) || !(str.startsWith("\"") && str.endsWith("\""))) return str;

  new StreamTokenizer(new StringReader(str));
  StreamTokenizer parser = new StreamTokenizer(new StringReader(str));
  String result;

  try {
    parser.nextToken();
    if (parser.ttype == '"') {
      result = parser.sval;
    } else {
      result = "ERROR!";
    }
  } catch (IOException e) {
    result = null;

    LoggerHelper.info("Encountered an IOException while parsing the input string", e);
  }

  return result;
}
 
Example 4
Source File: KeyStoreUtil.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Parses a option line likes
 *    -genkaypair -dname "CN=Me"
 * and add the results into a list
 * @param list the list to fill into
 * @param s the line
 */
private static void parseArgsLine(List<String> list, String s)
        throws IOException, PropertyExpander.ExpandException {
    StreamTokenizer st = new StreamTokenizer(new StringReader(s));

    st.resetSyntax();
    st.whitespaceChars(0x00, 0x20);
    st.wordChars(0x21, 0xFF);
    // Everything is a word char except for quotation and apostrophe
    st.quoteChar('"');
    st.quoteChar('\'');

    while (true) {
        if (st.nextToken() == StreamTokenizer.TT_EOF) {
            break;
        }
        list.add(PropertyExpander.expand(st.sval));
    }
}
 
Example 5
Source File: Harness.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
String[] parseBenchArgs(StreamTokenizer tokens)
    throws IOException, ConfigFormatException
{
    Vector vec = new Vector();
    for (;;) {
        switch (tokens.ttype) {
            case StreamTokenizer.TT_EOF:
            case StreamTokenizer.TT_EOL:
                return (String[]) vec.toArray(new String[vec.size()]);

            case StreamTokenizer.TT_WORD:
            case '"':
                vec.add(tokens.sval);
                tokens.nextToken();
                break;

            default:
                throw new ConfigFormatException("unrecognized arg token " +
                        "on line " + tokens.lineno());
        }
    }
}
 
Example 6
Source File: Harness.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
String[] parseBenchArgs(StreamTokenizer tokens)
    throws IOException, ConfigFormatException
{
    Vector vec = new Vector();
    for (;;) {
        switch (tokens.ttype) {
            case StreamTokenizer.TT_EOF:
            case StreamTokenizer.TT_EOL:
                return (String[]) vec.toArray(new String[vec.size()]);

            case StreamTokenizer.TT_WORD:
            case '"':
                vec.add(tokens.sval);
                tokens.nextToken();
                break;

            default:
                throw new ConfigFormatException("unrecognized arg token " +
                        "on line " + tokens.lineno());
        }
    }
}
 
Example 7
Source File: ProtocolTest.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Read an int from the command file
 * Negative numbers are preceded by "-"
 */
private int getInt(StreamTokenizer tkn) throws IOException
{
    int mult = 1;
    int val = tkn.nextToken();
    if (tkn.sval != null && tkn.sval.equals("-"))
    {
        mult = -1;
        val = tkn.nextToken();
    }

    if (val != StreamTokenizer.TT_NUMBER)
    {
        assertNotNull("Invalid string on line " + ln(tkn), tkn.sval);
        String str = tkn.sval.toLowerCase(Locale.ENGLISH);
        if (!str.startsWith("0x")) {
            fail("Expecting number, got " + tkn.sval + " on line " +
                    ln(tkn));
        } else {
            return convertHex(str, ln(tkn));
        }
    }
    return (new Double(tkn.nval).intValue() * mult);
}
 
Example 8
Source File: AttributeReaderState.java    From reladomo with Apache License 2.0 5 votes vote down vote up
private void parseAttributes(StreamTokenizer st) throws IOException, ParseException
{
    int token = st.nextToken();
    boolean wantAttribute = true;
    while(token != StreamTokenizer.TT_EOL)
    {
        if (wantAttribute)
        {
            if (token != StreamTokenizer.TT_WORD)
            {
                throw new ParseException("expected an attribute name on line "+st.lineno(), st.lineno());
            }
            this.getParser().getCurrentParsedData().addAttribute(st.sval, st.lineno());
        }
        else
        {
            if (token != ',')
            {
                throw new ParseException("Expected a comma on line "+st.lineno(), st.lineno());
            }
        }
        wantAttribute = !wantAttribute;
        token = st.nextToken();
    }
    if (wantAttribute)
    {
        throw new ParseException("extra comma at the end of line "+st.lineno(), st.lineno());
    }
}
 
Example 9
Source File: Tools.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
/** Delivers the next token and skip empty lines. */
public static void getFirstToken(StreamTokenizer tokenizer) throws IOException {

	while (tokenizer.nextToken() == StreamTokenizer.TT_EOL) {
		// skip empty lines
	}

	if (tokenizer.ttype == '\'' || tokenizer.ttype == '"') {
		tokenizer.ttype = StreamTokenizer.TT_WORD;
	} else if (tokenizer.ttype == StreamTokenizer.TT_WORD && tokenizer.sval.equals("?")) {
		tokenizer.ttype = '?';
	}
}
 
Example 10
Source File: Tools.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
/** Skips all tokens before next end of line (EOL). */
public static void waitForEOL(StreamTokenizer tokenizer) throws IOException {

	while (tokenizer.nextToken() != StreamTokenizer.TT_EOL) {
		// skip everything until EOL
	}
	tokenizer.pushBack();
}
 
Example 11
Source File: ParseExpression.java    From icafe with Eclipse Public License 1.0 5 votes vote down vote up
static Expression element(StreamTokenizer st) throws SyntaxError {
    Expression result = null;

    try {
        switch (st.nextToken()) {
            case StreamTokenizer.TT_NUMBER :
                result = new ConstantExpression(st.nval);
                break;

            case StreamTokenizer.TT_WORD :
                result = new VariableExpression(st.sval);
                break;
            case '(' :
                result = expression(st);
                st.nextToken();
                if (st.ttype != ')') {
                    st.pushBack();
                    throw new SyntaxError("Mismatched parenthesis.");
                }
                break;
            default:
                st.pushBack();
                throw new SyntaxError("Unexpected symbol on input.");
        }
    } catch (IOException ioe) {
        throw new SyntaxError("Caught an I/O exception.");
    }
    return result;
}
 
Example 12
Source File: Tools.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
/** Delivers the next token and checks for an unexpected end of line or file. */
public static void getNextToken(StreamTokenizer tokenizer) throws IOException {
	if (tokenizer.nextToken() == StreamTokenizer.TT_EOL) {
		throw new IOException("unexpected end of line " + tokenizer.lineno());
	}

	if (tokenizer.ttype == StreamTokenizer.TT_EOF) {
		throw new IOException("unexpected end of file in line " + tokenizer.lineno());
	} else if (tokenizer.ttype == '\'' || tokenizer.ttype == '"') {
		tokenizer.ttype = StreamTokenizer.TT_WORD;
	} else if (tokenizer.ttype == StreamTokenizer.TT_WORD && tokenizer.sval.equals("?")) {
		tokenizer.ttype = '?';
	}
}
 
Example 13
Source File: Harness.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
BenchInfo parseBenchInfo(StreamTokenizer tokens)
    throws IOException, ConfigFormatException
{
    float weight = parseBenchWeight(tokens);
    String name = parseBenchName(tokens);
    Benchmark bench = parseBenchClass(tokens);
    String[] args = parseBenchArgs(tokens);
    if (tokens.ttype == StreamTokenizer.TT_EOL)
        tokens.nextToken();
    return new BenchInfo(bench, name, weight, args);
}
 
Example 14
Source File: Harness.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
BenchInfo parseBenchInfo(StreamTokenizer tokens)
    throws IOException, ConfigFormatException
{
    float weight = parseBenchWeight(tokens);
    String name = parseBenchName(tokens);
    Benchmark bench = parseBenchClass(tokens);
    String[] args = parseBenchArgs(tokens);
    if (tokens.ttype == StreamTokenizer.TT_EOL)
        tokens.nextToken();
    return new BenchInfo(bench, name, weight, args);
}
 
Example 15
Source File: CaseSelectorBeginParserState.java    From reladomo with Apache License 2.0 4 votes vote down vote up
@Override
public ComputedAttributeParserState parse(StreamTokenizer st) throws IOException, ParseException
{
    ComputedAttributeParserState nextState = null;
    while(nextState == null && st.ttype != StreamTokenizer.TT_EOF)
    {
        int nextToken = st.nextToken();
        if (nextToken != StreamTokenizer.TT_EOL && nextToken != StreamTokenizer.TT_EOF)
        {
            ArrayList<Expression> stack = this.getParser().getStateStack();
            CaseExpression caseExpression = (CaseExpression) stack.get(stack.size() - 1);
            switch(nextToken)
            {
                case StreamTokenizer.TT_NUMBER:
                    caseExpression.addNumberKey(st.nval);
                    nextState = new CaseSelectorMiddleParserState(this.getParser());
                    break;
                case StreamTokenizer.TT_WORD:
                    if (st.sval.equals("null"))
                    {
                        caseExpression.addNullKey();
                        stack.add(new NullExpression());
                    }
                    else if (st.sval.equals("true") || st.sval.equals("false"))
                    {
                        caseExpression.addBooleanKey(Boolean.valueOf(st.sval));
                        stack.add(new NullExpression());
                    }
                    else if (st.sval.equals("default"))
                    {
                        caseExpression.addDefaultKey();
                        stack.add(new NullExpression());
                    }
                    else
                    {
                        throw new ParseException("unexpected word "+st.sval+" in expression "+this.getParser().getFormula()+" in "+this.getParser().getDiagnosticMessage());
                    }
                    nextState = new CaseSelectorMiddleParserState(this.getParser());
                    break;
                case '"':
                    caseExpression.addStringConstantKey(st.sval);
                    nextState = new CaseSelectorMiddleParserState(this.getParser());
                    break;
                default:
                    char ch = (char)st.ttype;
                    throw createUnexpectedCharacterException(ch, "<number>|<null>|<boolean>|<default>|<string>");
            }
        }
    }
    return nextState;
}
 
Example 16
Source File: XYZApp.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/** Create a Chemical model by parsing an input stream */
XYZChemModel(InputStream is) throws Exception {
    this();
    StreamTokenizer st = new StreamTokenizer(
            new BufferedReader(new InputStreamReader(is, "UTF-8")));
    st.eolIsSignificant(true);
    st.commentChar('#');

    try {
        scan:
        while (true) {
            switch (st.nextToken()) {
                case StreamTokenizer.TT_EOF:
                    break scan;
                default:
                    break;
                case StreamTokenizer.TT_WORD:
                    String name = st.sval;
                    double x = 0,
                     y = 0,
                     z = 0;
                    if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
                        x = st.nval;
                        if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
                            y = st.nval;
                            if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
                                z = st.nval;
                            }
                        }
                    }
                    addVert(name, (float) x, (float) y, (float) z);
                    while (st.ttype != StreamTokenizer.TT_EOL
                            && st.ttype != StreamTokenizer.TT_EOF) {
                        st.nextToken();
                    }

            }   // end Switch

        }  // end while

        is.close();

    } // end Try
    catch (IOException e) {
    }

    if (st.ttype != StreamTokenizer.TT_EOF) {
        throw new Exception(st.toString());
    }

}
 
Example 17
Source File: RunCART.java    From KEEL with GNU General Public License v3.0 4 votes vote down vote up
/** Function to read the options from the execution file and assign
 * the values to the parameters.
 *
 * @param options 		The StreamTokenizer that reads the parameters file.
 *
 * @throws Exception	If the format of the file is not correct.
 */
protected void setOptions(StreamTokenizer options) throws Exception {
	options.nextToken();

	/* Checks that the file starts with the token algorithm */
	if (options.sval.equalsIgnoreCase("algorithm")) {
		options.nextToken();
		options.nextToken();

		/* Check algorithm name
		if (!options.sval.equalsIgnoreCase("CART")) {
			throw new Exception("The name of the algorithm is not correct.");
		}
		 */

		options.nextToken();
		options.nextToken();

		/* Reads the names of the input files*/
		if (options.sval.equalsIgnoreCase("inputData")) {
			options.nextToken();
			options.nextToken();
			modelFileName = options.sval;

			if (options.nextToken() != StreamTokenizer.TT_EOL) {
				trainFileName = options.sval;
				options.nextToken();
				testFileName = options.sval;
				if (options.nextToken() != StreamTokenizer.TT_EOL) {
					trainFileName = modelFileName;
					options.nextToken();
				}
			}
		} else {
			throw new Exception("No file test provided.");
		}

		/* Reads the names of the output files*/
		while (true) {
			if (options.nextToken() == StreamTokenizer.TT_EOF) {
				throw new Exception("No output file provided.");
			}

			if (options.sval == null) {
				continue;
			} else if (options.sval.equalsIgnoreCase("outputData")) {
				break;
			}
		}

		options.nextToken();
		options.nextToken();
		trainOutputFileName = options.sval;
		options.nextToken();
		testOutputFileName = options.sval;
		options.nextToken();
		resultFileName = options.sval;

		if (!getNextToken(options)) {
			throw new Exception("No instances provided.");
		}

		if (options.ttype == StreamTokenizer.TT_EOF) {
			return;
		}

		for (int k = 0; k < nParam; k++) {

			/* Reads the maxDepth parameter */
			if (options.sval.equalsIgnoreCase("maxDepth")) {
				options.nextToken();
				options.nextToken();

				if (Integer.parseInt(options.sval) > 0) {
					maxDepth = Integer.parseInt(options.sval);
				}

				if (!getNextToken(options)) {
					return;
				} else {
					continue;
				}
			}
			
			/* Any other parameter should be added here */

		} // end for
	} else {
		throw new Exception("The file must start with the word " +
		"algorithm followed of the name of the algorithm.");
	}
}
 
Example 18
Source File: XYZApp.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/** Create a Chemical model by parsing an input stream */
XYZChemModel(InputStream is) throws Exception {
    this();
    StreamTokenizer st = new StreamTokenizer(
            new BufferedReader(new InputStreamReader(is, "UTF-8")));
    st.eolIsSignificant(true);
    st.commentChar('#');

    try {
        scan:
        while (true) {
            switch (st.nextToken()) {
                case StreamTokenizer.TT_EOF:
                    break scan;
                default:
                    break;
                case StreamTokenizer.TT_WORD:
                    String name = st.sval;
                    double x = 0,
                     y = 0,
                     z = 0;
                    if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
                        x = st.nval;
                        if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
                            y = st.nval;
                            if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
                                z = st.nval;
                            }
                        }
                    }
                    addVert(name, (float) x, (float) y, (float) z);
                    while (st.ttype != StreamTokenizer.TT_EOL
                            && st.ttype != StreamTokenizer.TT_EOF) {
                        st.nextToken();
                    }

            }   // end Switch

        }  // end while

        is.close();

    } // end Try
    catch (IOException e) {
    }

    if (st.ttype != StreamTokenizer.TT_EOF) {
        throw new Exception(st.toString());
    }

}
 
Example 19
Source File: Command.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
void parseCommand(StreamTokenizer in) throws IOException {
    if (in.nextToken() != StreamTokenizer.TT_WORD) {
        throw new IOException("Unexpected token: " + in.ttype);
    }
    String word;
    String[] aliasBuf = null;
    int aliasPos;
    if (aliases != null
            && (aliasBuf = (String[]) aliases.get(in.sval)) != null) {
        word = aliasBuf[0];
        aliasPos = 1;
    } else {
        word = in.sval;
        aliasPos = 0;
    }
    if ("".equals(groupName) || word.startsWith("/")) {
        if (word.startsWith("/"))
            word = word.substring(1);
        commandGroupRef = matchCommandGroup(bc, word);
        word = null;
    } else {
        if (SessionCommandGroup.NAME.equals(groupName)) {
            commandGroupRef = null;
        } else {
            Collection<ServiceReference<CommandGroup>> refs = null;
            try {
                refs = bc.getServiceReferences(CommandGroup.class,
                                               "(groupName=" + groupName + ")");
            } catch (InvalidSyntaxException ignore) {
            }
            if (refs.isEmpty()) {
                throw new IOException("No such command group: " + groupName);
            }
            commandGroupRef = refs.iterator().next();
        }
    }
    ArrayList<String> vargs = new ArrayList<String>();
    boolean done = false;
    while (!done) {
        if (word != null) {
            vargs.add(word);
            word = null;
        } else if (aliasPos > 0) {
            if (aliasPos < aliasBuf.length) {
                word = aliasBuf[aliasPos++];
            } else {
                aliasPos = 0;
            }
        } else {
            switch (in.nextToken()) {
            case '|':
                out = new Pipe();
                isPiped = true;
                done = true;
                break;
            case '&':
                isBackground = true;
            case ';':
                done = true;
                break;
            case '<':
                if (in.nextToken() != StreamTokenizer.TT_WORD) {
                    throw new IOException("Empty input redirect");
                }
                // Set input to file
                break;
            case '>':
                if (in.nextToken() != StreamTokenizer.TT_WORD) {
                    throw new IOException("Empty output redirect");
                }
                // Set output to file
                break;
            case StreamTokenizer.TT_EOL:
            case StreamTokenizer.TT_EOF:
                in.pushBack();
                done = true;
                break;
            case '"':
            case '\'':
            case StreamTokenizer.TT_WORD:
                word = in.sval;
                break;
            default:
                throw new IOException("Unknown token: " + in.ttype);
            }
        }
    }
    args = vargs.toArray(new String[vargs.size()]);
}
 
Example 20
Source File: Harness.java    From openjdk-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create new benchmark harness with given configuration and reporter.
 * Throws ConfigFormatException if there was an error parsing the config
 * file.
 * <p>
 * <b>Config file syntax:</b>
 * <p>
 * '#' marks the beginning of a comment.  Blank lines are ignored.  All
 * other lines should adhere to the following format:
 * <pre>
 *     &lt;weight&gt; &lt;name&gt; &lt;class&gt; [&lt;args&gt;]
 * </pre>
 * &lt;weight&gt; is a floating point value which is multiplied times the
 * benchmark's execution time to determine its weighted score.  The
 * total score of the benchmark suite is the sum of all weighted scores
 * of its benchmarks.
 * <p>
 * &lt;name&gt; is a name used to identify the benchmark on the benchmark
 * report.  If the name contains whitespace, the quote character '"' should
 * be used as a delimiter.
 * <p>
 * &lt;class&gt; is the full name (including the package) of the class
 * containing the benchmark implementation.  This class must implement
 * bench.Benchmark.
 * <p>
 * [&lt;args&gt;] is a variable-length list of runtime arguments to pass to
 * the benchmark.  Arguments containing whitespace should use the quote
 * character '"' as a delimiter.
 * <p>
 * <b>Example:</b>
 * <pre>
 *      3.5 "My benchmark" bench.serial.Test first second "third arg"
 * </pre>
 */
public Harness(InputStream in) throws IOException, ConfigFormatException {
    Vector bvec = new Vector();
    StreamTokenizer tokens = new StreamTokenizer(new InputStreamReader(in));

    tokens.resetSyntax();
    tokens.wordChars(0, 255);
    tokens.whitespaceChars(0, ' ');
    tokens.commentChar('#');
    tokens.quoteChar('"');
    tokens.eolIsSignificant(true);

    tokens.nextToken();
    while (tokens.ttype != StreamTokenizer.TT_EOF) {
        switch (tokens.ttype) {
            case StreamTokenizer.TT_WORD:
            case '"':                       // parse line
                bvec.add(parseBenchInfo(tokens));
                break;

            default:                        // ignore
                tokens.nextToken();
                break;
        }
    }
    binfo = (BenchInfo[]) bvec.toArray(new BenchInfo[bvec.size()]);
}