Example usage for java.io StreamTokenizer ordinaryChar

List of usage examples for java.io StreamTokenizer ordinaryChar

Introduction

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

Prototype

public void ordinaryChar(int ch) 

Source Link

Document

Specifies that the character argument is "ordinary" in this tokenizer.

Usage

From source file:org.openmrs.reporting.PatientSearch.java

public static PatientSearch createCompositionSearch(String description) {
    // TODO This is a rewrite of the code in CohortSearchHistory.createCompositionFilter(String). That method should probably delegate to this one in some way.
    // TODO use open/closeParenthesesWords declared above
    List<Object> tokens = new ArrayList<Object>();
    try {/*from  w w  w  .  ja v a2 s.co  m*/
        StreamTokenizer st = new StreamTokenizer(new StringReader(description));
        st.ordinaryChar('(');
        st.ordinaryChar(')');
        while (st.nextToken() != StreamTokenizer.TT_EOF) {
            if (st.ttype == StreamTokenizer.TT_NUMBER) {
                Integer thisInt = new Integer((int) st.nval);
                if (thisInt < 1) {
                    log.error("number < 1");
                    return null;
                }
                tokens.add(thisInt);
            } else if (st.ttype == '(') {
                tokens.add("(");
            } else if (st.ttype == ')') {
                tokens.add(")");
            } else if (st.ttype == StreamTokenizer.TT_WORD) {
                String str = st.sval.toLowerCase();
                tokens.add(str);
            }
        }
        return createCompositionSearch(tokens);
    } catch (Exception ex) {
        log.error("Error in description string: " + description, ex);
        return null;
    }
}

From source file:org.pentaho.big.data.kettle.plugins.sqoop.SqoopUtils.java

/**
 * Parse a string into arguments as if it were provided on the command line.
 *
 * @param commandLineString/*from w w  w .  j  a  v a  2s  .  c om*/
 *          A command line string, e.g. "sqoop import --table test --connect jdbc:mysql://bogus/bogus"
 * @param variableSpace
 *          Context for resolving variable names. If {@code null}, no variable resolution we happen.
 * @param ignoreSqoopCommand
 *          If set, the first "sqoop <tool>" arguments will be ignored, e.g. "sqoop import" or "sqoop export".
 * @return List of parsed arguments
 * @throws IOException
 *           when the command line could not be parsed
 */
public static List<String> parseCommandLine(String commandLineString, VariableSpace variableSpace,
        boolean ignoreSqoopCommand) throws IOException {
    List<String> args = new ArrayList<String>();
    StringReader reader = new StringReader(commandLineString);
    try {
        StreamTokenizer tokenizer = new StreamTokenizer(reader);
        // Treat a dash as an ordinary character so it gets included in the token
        tokenizer.ordinaryChar('-');
        tokenizer.ordinaryChar('.');
        tokenizer.ordinaryChars('0', '9');
        // Treat all characters as word characters so nothing is parsed out
        tokenizer.wordChars('\u0000', '\uFFFF');

        // Re-add whitespace characters
        tokenizer.whitespaceChars(0, ' ');

        // Use " and ' as quote characters
        tokenizer.quoteChar('"');
        tokenizer.quoteChar('\'');

        // Flag to indicate if the next token needs to be skipped (used to control skipping of the first two arguments,
        // e.g. "sqoop <tool>")
        boolean skipToken = false;
        // Add all non-null string values tokenized from the string to the argument list
        while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
            if (tokenizer.sval != null) {
                String s = tokenizer.sval;
                if (variableSpace != null) {
                    s = variableSpace.environmentSubstitute(s);
                }
                if (ignoreSqoopCommand && args.isEmpty()) {
                    // If we encounter "sqoop <name>" we should skip the first two arguments so we can support copy/paste of
                    // arguments directly
                    // from a working command line
                    if ("sqoop".equals(s)) {
                        skipToken = true;
                        continue; // skip this one and the next
                    } else if (skipToken) {
                        ignoreSqoopCommand = false; // Don't attempt to ignore any more commands
                        // Skip this token too, reset the flag so we no longer skip any tokens, and continue parsing
                        skipToken = false;
                        continue;
                    }
                }

                if (s.startsWith(ARG_D)) {
                    handleCustomOption(args, s, tokenizer, variableSpace);
                    continue;
                }
                args.add(escapeEscapeSequences(s));
            }
        }
    } finally {
        reader.close();
    }
    return args;
}

From source file:org.pentaho.di.job.entries.spark.JobEntrySparkSubmit.java

/**
 * Parse a string into arguments as if it were provided on the command line.
 *
 * @param commandLineString A command line string.
 * @return List of parsed arguments//from www .  j a  v a2 s . co  m
 * @throws IOException when the command line could not be parsed
 */
public List<String> parseCommandLine(String commandLineString) throws IOException {
    List<String> args = new ArrayList<String>();
    StringReader reader = new StringReader(commandLineString);
    try {
        StreamTokenizer tokenizer = new StreamTokenizer(reader);
        // Treat a dash as an ordinary character so it gets included in the token
        tokenizer.ordinaryChar('-');
        tokenizer.ordinaryChar('.');
        tokenizer.ordinaryChars('0', '9');
        // Treat all characters as word characters so nothing is parsed out
        tokenizer.wordChars('\u0000', '\uFFFF');

        // Re-add whitespace characters
        tokenizer.whitespaceChars(0, ' ');

        // Use " and ' as quote characters
        tokenizer.quoteChar('"');
        tokenizer.quoteChar('\'');

        // Add all non-null string values tokenized from the string to the argument list
        while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
            if (tokenizer.sval != null) {
                String s = tokenizer.sval;
                s = environmentSubstitute(s);
                args.add(s);
            }
        }
    } finally {
        reader.close();
    }

    return args;
}

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);//from   ww  w  .j  a va  2s  .  com
    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);
}