Example usage for java.io StreamTokenizer TT_NUMBER

List of usage examples for java.io StreamTokenizer TT_NUMBER

Introduction

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

Prototype

int TT_NUMBER

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

Click Source Link

Document

A constant indicating that a number token has been read.

Usage

From source file:org.structr.core.function.Functions.java

public static Object evaluate(final ActionContext actionContext, final GraphObject entity,
        final String expression) throws FrameworkException {

    final String expressionWithoutNewlines = expression.replace('\n', ' ');
    final StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(expressionWithoutNewlines));
    tokenizer.eolIsSignificant(true);/*  ww  w .j a  v a 2  s .c o m*/
    tokenizer.ordinaryChar('.');
    tokenizer.wordChars('_', '_');
    tokenizer.wordChars('.', '.');
    tokenizer.wordChars('!', '!');

    Expression root = new RootExpression();
    Expression current = root;
    Expression next = null;
    String lastToken = null;
    int token = 0;
    int level = 0;

    while (token != StreamTokenizer.TT_EOF) {

        token = nextToken(tokenizer);

        switch (token) {

        case StreamTokenizer.TT_EOF:
            break;

        case StreamTokenizer.TT_EOL:
            break;

        case StreamTokenizer.TT_NUMBER:
            if (current == null) {
                throw new FrameworkException(422,
                        "Invalid expression: mismatched opening bracket before NUMBER");
            }
            next = new ConstantExpression(tokenizer.nval);
            current.add(next);
            lastToken += "NUMBER";
            break;

        case StreamTokenizer.TT_WORD:
            if (current == null) {
                throw new FrameworkException(422,
                        "Invalid expression: mismatched opening bracket before " + tokenizer.sval);
            }
            next = checkReservedWords(tokenizer.sval);
            Expression previousExpression = current.getPrevious();
            if (tokenizer.sval.startsWith(".") && previousExpression != null
                    && previousExpression instanceof FunctionExpression && next instanceof ValueExpression) {

                final FunctionExpression previousFunctionExpression = (FunctionExpression) previousExpression;
                final ValueExpression valueExpression = (ValueExpression) next;

                current.replacePrevious(
                        new FunctionValueExpression(previousFunctionExpression, valueExpression));
            } else {
                current.add(next);
            }
            lastToken = tokenizer.sval;
            break;

        case '(':
            if (((current == null || current instanceof RootExpression) && next == null) || current == next) {

                // an additional bracket without a new function,
                // this can only be an execution group.
                next = new GroupExpression();
                current.add(next);
            }

            current = next;
            lastToken += "(";
            level++;
            break;

        case ')':
            if (current == null) {
                throw new FrameworkException(422,
                        "Invalid expression: mismatched opening bracket before " + lastToken);
            }
            current = current.getParent();
            if (current == null) {
                throw new FrameworkException(422,
                        "Invalid expression: mismatched closing bracket after " + lastToken);
            }
            lastToken += ")";
            level--;
            break;

        case '[':
            // bind directly to the previous expression
            next = new ArrayExpression();
            current.add(next);
            current = next;
            lastToken += "[";
            level++;
            break;

        case ']':
            if (current == null) {
                throw new FrameworkException(422,
                        "Invalid expression: mismatched closing bracket before " + lastToken);
            }
            current = current.getParent();
            if (current == null) {
                throw new FrameworkException(422,
                        "Invalid expression: mismatched closing bracket after " + lastToken);
            }
            lastToken += "]";
            level--;
            break;

        case ';':
            next = null;
            lastToken += ";";
            break;

        case ',':
            next = current;
            lastToken += ",";
            break;

        default:
            if (current == null) {
                throw new FrameworkException(422,
                        "Invalid expression: mismatched opening bracket before " + tokenizer.sval);
            }
            current.add(new ConstantExpression(tokenizer.sval));
            lastToken = tokenizer.sval;

        }
    }

    if (level > 0) {
        throw new FrameworkException(422, "Invalid expression: mismatched closing bracket after " + lastToken);
    }

    return root.evaluate(actionContext, entity);
}

From source file:org.wso2.andes.server.security.access.config.PlainConfiguration.java

@Override
public RuleSet load() throws ConfigurationException {
    RuleSet ruleSet = super.load();

    try {//from w w w .ja v a 2  s.  c  om
        _st = new StreamTokenizer(new BufferedReader(new FileReader(_file)));
        _st.resetSyntax(); // setup the tokenizer

        _st.commentChar(COMMENT); // single line comments
        _st.eolIsSignificant(true); // return EOL as a token
        _st.lowerCaseMode(true); // case insensitive tokens
        _st.ordinaryChar('='); // equals is a token
        _st.ordinaryChar(CONTINUATION); // continuation character (when followed by EOL)
        _st.quoteChar('"'); // double quote
        _st.quoteChar('\''); // single quote
        _st.whitespaceChars('\u0000', '\u0020'); // whitespace (to be ignored) TODO properly
        _st.wordChars('a', 'z'); // unquoted token characters [a-z]
        _st.wordChars('A', 'Z'); // [A-Z]
        _st.wordChars('0', '9'); // [0-9]
        _st.wordChars('_', '_'); // underscore
        _st.wordChars('-', '-'); // dash
        _st.wordChars('.', '.'); // dot
        _st.wordChars('*', '*'); // star
        _st.wordChars('@', '@'); // at
        _st.wordChars(':', ':'); // colon

        // parse the acl file lines
        Stack<String> stack = new Stack<String>();
        int current;
        do {
            current = _st.nextToken();
            switch (current) {
            case StreamTokenizer.TT_EOF:
            case StreamTokenizer.TT_EOL:
                if (stack.isEmpty()) {
                    break; // blank line
                }

                // pull out the first token from the bottom of the stack and check arguments exist
                String first = stack.firstElement();
                stack.removeElementAt(0);
                if (stack.isEmpty()) {
                    throw new ConfigurationException(String.format(NOT_ENOUGH_TOKENS_MSG, getLine()));
                }

                // check for and parse optional initial number for ACL lines
                Integer number = null;
                if (StringUtils.isNumeric(first)) {
                    // set the acl number and get the next element
                    number = Integer.valueOf(first);
                    first = stack.firstElement();
                    stack.removeElementAt(0);
                }

                if (StringUtils.equalsIgnoreCase(ACL, first)) {
                    parseAcl(number, stack);
                } else if (number == null) {
                    if (StringUtils.equalsIgnoreCase(GROUP, first)) {
                        parseGroup(stack);
                    } else if (StringUtils.equalsIgnoreCase(CONFIG, first)) {
                        parseConfig(stack);
                    } else {
                        throw new ConfigurationException(
                                String.format(UNRECOGNISED_INITIAL_MSG, first, getLine()));
                    }
                } else {
                    throw new ConfigurationException(String.format(NUMBER_NOT_ALLOWED_MSG, first, getLine()));
                }

                // reset stack, start next line
                stack.clear();
                break;
            case StreamTokenizer.TT_NUMBER:
                stack.push(Integer.toString(Double.valueOf(_st.nval).intValue()));
                break;
            case StreamTokenizer.TT_WORD:
                stack.push(_st.sval); // token
                break;
            default:
                if (_st.ttype == CONTINUATION) {
                    int next = _st.nextToken();
                    if (next == StreamTokenizer.TT_EOL) {
                        break; // continue reading next line
                    }

                    // invalid location for continuation character (add one to line beacuse we ate the EOL)
                    throw new ConfigurationException(String.format(PREMATURE_CONTINUATION_MSG, getLine() + 1));
                } else if (_st.ttype == '\'' || _st.ttype == '"') {
                    stack.push(_st.sval); // quoted token
                } else {
                    stack.push(Character.toString((char) _st.ttype)); // single character
                }
            }
        } while (current != StreamTokenizer.TT_EOF);

        if (!stack.isEmpty()) {
            throw new ConfigurationException(String.format(PREMATURE_EOF_MSG, getLine()));
        }
    } catch (IllegalArgumentException iae) {
        throw new ConfigurationException(String.format(PARSE_TOKEN_FAILED_MSG, getLine()), iae);
    } catch (FileNotFoundException fnfe) {
        throw new ConfigurationException(String.format(CONFIG_NOT_FOUND_MSG, getFile().getName()), fnfe);
    } catch (IOException ioe) {
        throw new ConfigurationException(String.format(CANNOT_LOAD_MSG, getFile().getName()), ioe);
    }

    return ruleSet;
}

From source file:pipeline.misc_util.Utils.java

/**
 * Reads a text file formatted by the GSL histogram outputting functions:
 * range[0] range[1] bin[0]/*from ww w.  ja v  a2 s  .c o  m*/
 * range[1] range[2] bin[1]
 * range[2] range[3] bin[2]
 * ....
 * range[n-1] range[n] bin[n-1]
 * 
 * Writes the results as a pair of XY values into an XYSeries object from FreeChart,
 * taking the lower end of the range for each pair
 * 
 * @param fileName
 *            The path to the text file to read the data from
 * @param series
 *            The series object to write the results into
 */
public static void readHistogramIntoXYSeries(String fileName, XYSeries series) {
    final int nColumns = 3;
    // From http://www.java2s.com/Code/Java/File-Input-Output/ReadingNumbersfromaTextFile.htm
    try (Reader r = new BufferedReader(new FileReader(fileName))) {

        StreamTokenizer stok = new StreamTokenizer(r);
        stok.parseNumbers();
        int currentColumn = 0;
        double range0 = 0;
        stok.nextToken();

        while (stok.ttype != StreamTokenizer.TT_EOF) {
            if (stok.ttype == StreamTokenizer.TT_NUMBER) {
                if (currentColumn == 0) {
                    range0 = stok.nval;
                } else if (currentColumn == 2) {
                    series.add(range0, stok.nval);
                }
            } else
                Utils.log("Nonnumber while reading histogram from file " + fileName + ": " + stok.sval,
                        LogLevel.ERROR);
            stok.nextToken();
            currentColumn++;
            if (currentColumn == nColumns)
                currentColumn = 0;
        }
    } catch (FileNotFoundException e) {
        throw new RuntimeException("Could not find file " + fileName + " from which to read histogram", e);
    } catch (IOException e) {
        printStack(e);
    }

}

From source file:pipeline.misc_util.Utils.java

/**
 * Reads a text file formatted by the GSL histogram outputting functions:
 * range[0] range[1] bin[0]/*from  w  ww. j a va  2s . com*/
 * range[1] range[2] bin[1]
 * range[2] range[3] bin[2]
 * ....
 * range[n-1] range[n] bin[n-1]
 * 
 * Writes the results as a pair of XY values into an XYSeries object from FreeChart,
 * taking the lower end of the range for each pair
 * 
 * @param plot
 *            The plot produced by a plugin
 * @param series
 *            The series object to write the results into
 */
public static void readHistogramIntoXYSeries(PluginIOCells plot, XYSeries series) {
    final int nColumns = 3;
    // From http://www.java2s.com/Code/Java/File-Input-Output/ReadingNumbersfromaTextFile.htm
    byte[] protobufArray = (byte[]) plot.getProperty("Protobuf");
    if (protobufArray == null)
        return;
    Reader r = new StringReader(new String(protobufArray));
    StreamTokenizer stok = new StreamTokenizer(r);
    stok.parseNumbers();
    int currentColumn = 0;
    double range0 = 0;
    try {
        stok.nextToken();

        while (stok.ttype != StreamTokenizer.TT_EOF) {
            if (stok.ttype == StreamTokenizer.TT_NUMBER) {
                if (currentColumn == 0) {
                    range0 = stok.nval;
                } else if (currentColumn == 2) {
                    series.add(range0, stok.nval);
                }
            } else
                Utils.log("Nonnumber while reading histogram from string " + plot.getProperty("Protobuf") + ": "
                        + stok.sval, LogLevel.ERROR);
            stok.nextToken();
            currentColumn++;
            if (currentColumn == nColumns)
                currentColumn = 0;
        }
    } catch (IOException e) {
        printStack(e);
        return;
    }

}