Example usage for java.io StreamTokenizer TT_EOF

List of usage examples for java.io StreamTokenizer TT_EOF

Introduction

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

Prototype

int TT_EOF

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

Click Source Link

Document

A constant indicating that the end of the stream has been read.

Usage

From source file:org.mindswap.pellet.KRSSLoader.java

private ATermAppl parseExpr() throws IOException {
    ATermAppl a = null;/*w  w w  .j  a  va2s . c  o  m*/

    int token = in.nextToken();
    String s = in.sval;
    if (token == ':') {
        s = nextString();
        if (s.equalsIgnoreCase("TOP"))
            a = ATermUtils.TOP;
        else if (s.equalsIgnoreCase("BOTTOM"))
            a = ATermUtils.BOTTOM;
        else
            throw new RuntimeException("Parse exception after ':' " + s);
    } else if (token == '(') {
        token = in.nextToken();
        ATermUtils.assertTrue(token == StreamTokenizer.TT_WORD);

        s = in.sval;
        if (s.equalsIgnoreCase("NOT")) {
            ATermAppl c = parseExpr();
            a = ATermUtils.makeNot(c);

            if (ATermUtils.isPrimitive(c))
                kb.addClass(c);
        } else if (s.equalsIgnoreCase("AND")) {
            ATermList list = ATermUtils.EMPTY_LIST;

            while (!peekNext(')')) {
                ATermAppl c = parseExpr();

                if (ATermUtils.isPrimitive(c))
                    kb.addClass(c);
                list = list.insert(c);
            }
            a = ATermUtils.makeAnd(list);
        } else if (s.equalsIgnoreCase("OR")) {
            ATermList list = ATermUtils.EMPTY_LIST;

            while (!peekNext(')')) {
                ATermAppl c = parseExpr();

                if (ATermUtils.isPrimitive(c))
                    kb.addClass(c);
                list = list.insert(c);
            }
            a = ATermUtils.makeOr(list);
        } else if (s.equalsIgnoreCase("ONE-OF")) {
            ATermList list = ATermUtils.EMPTY_LIST;

            while (!peekNext(')')) {
                ATermAppl c = parseExpr();

                kb.addIndividual(c);
                list = list.insert(ATermUtils.makeValue(c));
            }
            a = ATermUtils.makeOr(list);
        } else if (s.equalsIgnoreCase("ALL")) {
            ATermAppl r = parseExpr();
            kb.addObjectProperty(r);
            ATermAppl c = parseExpr();
            if (ATermUtils.isPrimitive(c))
                kb.addClass(c);

            a = ATermUtils.makeAllValues(r, c);
        } else if (s.equalsIgnoreCase("SOME")) {
            ATermAppl r = parseExpr();
            kb.addObjectProperty(r);
            ATermAppl c = parseExpr();
            if (ATermUtils.isPrimitive(c))
                kb.addClass(c);
            a = ATermUtils.makeSomeValues(r, c);
        } else if (s.equalsIgnoreCase("AT-LEAST") || s.equalsIgnoreCase("ATLEAST")) {
            int n = nextInt();
            ATermAppl r = parseExpr();
            kb.addObjectProperty(r);

            a = ATermUtils.makeMin(r, n);
        } else if (s.equalsIgnoreCase("AT-MOST") || s.equalsIgnoreCase("ATMOST")) {
            int n = nextInt();
            ATermAppl r = parseExpr();
            kb.addObjectProperty(r);
            a = ATermUtils.makeMax(r, n);
        } else if (s.equalsIgnoreCase("A")) {
            ATermAppl r = nextTerm();
            // TODO what does term 'A' stand for
            kb.addProperty(r);
            kb.addFunctionalProperty(r);
            a = ATermUtils.makeMin(r, 1);
        } else if (s.equalsIgnoreCase("MIN") || s.equals(">=")) {
            ATermAppl r = nextTerm();
            kb.addDatatypeProperty(r);
            Object val = nextNumber();
            DatatypeReasoner dtReasoner = kb.getDatatypeReasoner();
            Datatype dt = xsdInteger.restrictMinInclusive(val);
            String dtName = dtReasoner.defineDatatype(dt);
            ATermAppl datatype = ATermUtils.makeTermAppl(dtName);
            a = ATermUtils.makeAllValues(r, datatype);
        } else if (s.equalsIgnoreCase("MAX") || s.equals("<=")) {
            ATermAppl r = nextTerm();
            kb.addDatatypeProperty(r);
            Object val = nextNumber();
            DatatypeReasoner dtReasoner = kb.getDatatypeReasoner();
            Datatype dt = xsdInteger.restrictMaxInclusive(val);
            String dtName = dtReasoner.defineDatatype(dt);
            ATermAppl datatype = ATermUtils.makeTermAppl(dtName);
            a = ATermUtils.makeAllValues(r, datatype);
        } else if (s.equals("=")) {
            ATermAppl r = nextTerm();
            kb.addDatatypeProperty(r);
            Object val = nextNumber();
            DatatypeReasoner dtReasoner = kb.getDatatypeReasoner();
            Datatype dt = xsdInteger.singleton(val);
            String dtName = dtReasoner.defineDatatype(dt);
            ATermAppl datatype = ATermUtils.makeTermAppl(dtName);
            a = ATermUtils.makeAllValues(r, datatype);
        } else if (s.equalsIgnoreCase("EXACTLY")) {
            int n = nextInt();
            ATermAppl r = parseExpr();
            kb.addObjectProperty(r);
            a = ATermUtils.makeAnd(ATermUtils.makeMax(r, n), ATermUtils.makeMin(r, n));
        } else if (s.equalsIgnoreCase("INV")) {
            ATermAppl r = parseExpr();
            kb.addObjectProperty(r);
            a = kb.getProperty(r).getInverse().getName();
        } else {
            throw new RuntimeException("Unknown expression " + s);
        }

        if (in.nextToken() != ')') {
            if (s.equalsIgnoreCase("AT-LEAST") || s.equalsIgnoreCase("AT-MOST") || s.equalsIgnoreCase("ATLEAST")
                    || s.equalsIgnoreCase("ATMOST")) {
                s = nextString();
                if (s.equalsIgnoreCase("TOP") || s.equalsIgnoreCase("*TOP*") || s.equalsIgnoreCase(":TOP"))
                    skipNext(')');
                else
                    throw new UnsupportedFeatureException("Qualified cardinality restrictions");
            } else
                throw new RuntimeException("Parse exception at term " + s);
        }
    } else if (token == '#') {
        int n = nextInt();
        if (peekNext('#')) {
            skipNext();
            a = (ATermAppl) terms.get(n);
            if (a == null)
                throw new RuntimeException("Parse exception: #" + n + "# is not defined");
        } else {
            skipNext("=");
            a = parseExpr();

            while (terms.size() <= n)
                terms.add(null);

            terms.set(n, a);
        }
    } else if (token == StreamTokenizer.TT_EOF)
        a = null;
    else if (s.equalsIgnoreCase("TOP") || s.equalsIgnoreCase("*TOP*") || s.equalsIgnoreCase(":TOP"))
        a = ATermUtils.TOP;
    else if (s.equalsIgnoreCase("BOTTOM") || s.equalsIgnoreCase("*BOTTOM*"))
        a = ATermUtils.BOTTOM;
    else {
        if (forceUppercase)
            s = s.toUpperCase();
        a = ATermUtils.makeTermAppl(s);
    }

    return a;
}

From source file:org.mindswap.pellet.KRSSLoader.java

public void load(Reader reader, KnowledgeBase kb) throws IOException {
    this.kb = kb;
    this.xsdInteger = (XSDAtomicType) kb.getDatatypeReasoner().getDatatype(Namespaces.XSD + "integer");

    initTokenizer(reader);/*from   www .j  a v a2 s. co m*/

    terms = new ArrayList();
    disjoints = new HashMap();

    int token = in.nextToken();
    while (token != StreamTokenizer.TT_EOF) {
        if (token == '#') {
            in.ordinaryChar('|');
            token = in.nextToken();
            while (token != '#')
                token = in.nextToken();
            in.quoteChar('|');
            token = in.nextToken();
            if (token == StreamTokenizer.TT_EOF)
                break;
        }
        if (token != '(')
            throw new RuntimeException("Expecting '(' but found " + in);

        String str = nextString();
        if (str.equalsIgnoreCase("DEFINE-PRIMITIVE-ROLE") || str.equalsIgnoreCase("DEFPRIMROLE")
                || str.equalsIgnoreCase("DEFINE-PRIMITIVE-ATTRIBUTE")
                || str.equalsIgnoreCase("DEFPRIMATTRIBUTE")
                || str.equalsIgnoreCase("DEFINE-DATATYPE-PROPERTY")) {
            ATermAppl r = nextTerm();

            boolean dataProp = str.equalsIgnoreCase("DEFINE-DATATYPE-PROPERTY");
            boolean functional = str.equalsIgnoreCase("DEFINE-PRIMITIVE-ATTRIBUTE")
                    || str.equalsIgnoreCase("DEFPRIMATTRIBUTE");

            if (dataProp) {
                kb.addDatatypeProperty(r);
                if (log.isDebugEnabled())
                    log.debug("DEFINE-DATATYPE-ROLE " + r);
            } else {
                kb.addObjectProperty(r);
                if (functional) {
                    kb.addFunctionalProperty(r);
                    if (log.isDebugEnabled())
                        log.debug("DEFINE-PRIMITIVE-ATTRIBUTE " + r);
                } else if (log.isDebugEnabled())
                    log.debug("DEFINE-PRIMITIVE-ROLE " + r);
            }

            while (!peekNext(')')) {
                if (peekNext(':')) {
                    skipNext(':');
                    String cmd = nextString();
                    if (cmd.equalsIgnoreCase("parents")) {
                        boolean paren = peekNext('(');
                        if (paren) {
                            skipNext('(');
                            while (!peekNext(')')) {
                                ATermAppl s = nextTerm();
                                if (!s.getName().equals("NIL")) {
                                    kb.addObjectProperty(s);
                                    kb.addSubProperty(r, s);
                                    if (log.isDebugEnabled())
                                        log.debug("PARENT-ROLE " + r + " " + s);
                                }
                            }
                            skipNext(')');
                        } else {
                            ATermAppl s = nextTerm();
                            if (!s.toString().equalsIgnoreCase("NIL")) {
                                kb.addObjectProperty(s);
                                kb.addSubProperty(r, s);
                                if (log.isDebugEnabled())
                                    log.debug("PARENT-ROLE " + r + " " + s);
                            }
                        }
                    } else if (cmd.equalsIgnoreCase("transitive")) {
                        ATermUtils.assertTrue(nextString().equalsIgnoreCase("T"));
                        kb.addTransitiveProperty(r);
                        if (log.isDebugEnabled())
                            log.debug("TRANSITIVE-ROLE " + r);
                    } else if (cmd.equalsIgnoreCase("range")) {
                        ATermAppl range = parseExpr();
                        kb.addClass(range);
                        kb.addRange(r, range);
                        if (log.isDebugEnabled())
                            log.debug("RANGE " + r + " " + range);
                    } else if (cmd.equalsIgnoreCase("domain")) {
                        ATermAppl domain = parseExpr();
                        kb.addClass(domain);
                        kb.addDomain(r, domain);
                        if (log.isDebugEnabled())
                            log.debug("DOMAIN " + r + " " + domain);
                    } else if (cmd.equalsIgnoreCase("inverse")) {
                        ATermAppl inv = nextTerm();
                        kb.addInverseProperty(r, inv);
                        if (log.isDebugEnabled())
                            log.debug("INVERSE " + r + " " + inv);
                    } else
                        throw new RuntimeException("Invalid role spec " + cmd);
                } else if (peekNext('(')) {
                    skipNext('(');
                    String cmd = nextString();
                    if (cmd.equalsIgnoreCase("domain-range")) {
                        ATermAppl domain = nextTerm();
                        ATermAppl range = nextTerm();

                        kb.addDomain(r, domain);
                        kb.addRange(r, range);
                        if (log.isDebugEnabled())
                            log.debug("DOMAIN-RANGE " + r + " " + domain + " " + range);
                    } else
                        throw new RuntimeException("Invalid role spec");
                    skipNext(')');
                }
            }
        } else if (str.equalsIgnoreCase("DEFINE-PRIMITIVE-CONCEPT") || str.equalsIgnoreCase("DEFPRIMCONCEPT")) {
            ATermAppl c = nextTerm();
            kb.addClass(c);

            ATermAppl expr = null;
            if (!peekNext(')')) {
                expr = parseExpr();

                if (!expr.getName().equals("NIL")) {
                    kb.addClass(expr);
                    kb.addSubClass(c, expr);
                }
            }

            if (log.isDebugEnabled())
                log.debug("DEFINE-PRIMITIVE-CONCEPT " + c + " " + (expr == null ? "" : expr.toString()));
        } else if (str.equalsIgnoreCase("DEFINE-DISJOINT-PRIMITIVE-CONCEPT")) {
            ATermAppl c = nextTerm();
            kb.addClass(c);

            skipNext('(');
            while (!peekNext(')')) {
                ATermAppl expr = parseExpr();

                List prevDefinitions = (List) disjoints.get(expr);
                if (prevDefinitions == null)
                    prevDefinitions = new ArrayList();
                for (Iterator i = prevDefinitions.iterator(); i.hasNext();) {
                    ATermAppl d = (ATermAppl) i.next();
                    kb.addDisjointClass(c, d);
                    if (log.isDebugEnabled())
                        log.debug("DEFINE-PRIMITIVE-DISJOINT " + c + " " + d);
                }
                prevDefinitions.add(c);
            }
            skipNext(')');

            ATermAppl expr = parseExpr();
            kb.addSubClass(c, expr);

            if (log.isDebugEnabled())
                log.debug("DEFINE-PRIMITIVE-CONCEPT " + c + " " + expr);
        } else if (str.equalsIgnoreCase("DEFINE-CONCEPT") || str.equalsIgnoreCase("DEFCONCEPT")
                || str.equalsIgnoreCase("EQUAL_C")) {
            ATermAppl c = nextTerm();
            kb.addClass(c);

            ATermAppl expr = parseExpr();
            kb.addEquivalentClass(c, expr);

            if (log.isDebugEnabled())
                log.debug("DEFINE-CONCEPT " + c + " " + expr);
        } else if (str.equalsIgnoreCase("IMPLIES") || str.equalsIgnoreCase("IMPLIES_C")) {
            ATermAppl c1 = parseExpr();
            ATermAppl c2 = parseExpr();
            kb.addClass(c1);
            kb.addClass(c2);
            kb.addSubClass(c1, c2);

            if (log.isDebugEnabled())
                log.debug("IMPLIES " + c1 + " " + c2);
        } else if (str.equalsIgnoreCase("IMPLIES_R")) {
            ATermAppl p1 = parseExpr();
            ATermAppl p2 = parseExpr();
            kb.addProperty(p1);
            kb.addProperty(p2);
            kb.addSubProperty(p1, p2);

            if (log.isDebugEnabled())
                log.debug("IMPLIES_R " + p1 + " " + p2);
        } else if (str.equalsIgnoreCase("EQUAL_R")) {
            ATermAppl p1 = parseExpr();
            ATermAppl p2 = parseExpr();
            kb.addObjectProperty(p1);
            kb.addObjectProperty(p2);
            kb.addEquivalentProperty(p1, p2);

            if (log.isDebugEnabled())
                log.debug("EQUAL_R " + p1 + " " + p2);
        } else if (str.equalsIgnoreCase("DOMAIN")) {
            ATermAppl p = parseExpr();
            ATermAppl c = parseExpr();
            kb.addProperty(p);
            kb.addClass(c);
            kb.addDomain(p, c);

            if (log.isDebugEnabled())
                log.debug("DOMAIN " + p + " " + c);
        } else if (str.equalsIgnoreCase("RANGE")) {
            ATermAppl p = parseExpr();
            ATermAppl c = parseExpr();
            kb.addProperty(p);
            kb.addClass(c);
            kb.addRange(p, c);

            if (log.isDebugEnabled())
                log.debug("RANGE " + p + " " + c);
        } else if (str.equalsIgnoreCase("FUNCTIONAL")) {
            ATermAppl p = parseExpr();
            kb.addProperty(p);
            kb.addFunctionalProperty(p);

            if (log.isDebugEnabled())
                log.debug("FUNCTIONAL " + p);
        } else if (str.equalsIgnoreCase("TRANSITIVE")) {
            ATermAppl p = parseExpr();
            kb.addObjectProperty(p);
            kb.addTransitiveProperty(p);

            if (log.isDebugEnabled())
                log.debug("TRANSITIVE " + p);
        } else if (str.equalsIgnoreCase("DISJOINT")) {
            ATermAppl[] list = parseExprList();
            for (int i = 0; i < list.length - 1; i++) {
                ATermAppl c1 = list[i];
                for (int j = i + 1; j < list.length; j++) {
                    ATermAppl c2 = list[j];
                    kb.addClass(c2);
                    kb.addDisjointClass(c1, c2);
                    if (log.isDebugEnabled())
                        log.debug("DISJOINT " + c1 + " " + c2);
                }
            }
        } else if (str.equalsIgnoreCase("DEFINDIVIDUAL")) {
            ATermAppl x = nextTerm();

            kb.addIndividual(x);
            if (log.isDebugEnabled())
                log.debug("DEFINDIVIDUAL " + x);
        } else if (str.equalsIgnoreCase("INSTANCE")) {
            ATermAppl x = nextTerm();
            ATermAppl c = parseExpr();

            kb.addIndividual(x);
            kb.addType(x, c);
            if (log.isDebugEnabled())
                log.debug("INSTANCE " + x + " " + c);
        } else if (str.equalsIgnoreCase("RELATED")) {
            ATermAppl x = nextTerm();
            ATermAppl y = nextTerm();
            ATermAppl r = nextTerm();

            kb.addIndividual(x);
            kb.addIndividual(y);
            kb.addPropertyValue(r, x, y);

            if (log.isDebugEnabled())
                log.debug("RELATED " + x + " - " + r + " -> " + y);
        } else if (str.equalsIgnoreCase("DIFFERENT")) {
            ATermAppl x = nextTerm();
            ATermAppl y = nextTerm();

            kb.addIndividual(x);
            kb.addIndividual(y);
            kb.addDifferent(x, y);

            if (log.isDebugEnabled())
                log.debug("DIFFERENT " + x + " " + y);
        } else if (str.equalsIgnoreCase("DATATYPE-ROLE-FILLER")) {
            ATermAppl x = nextTerm();
            ATermAppl y = ATermUtils.makePlainLiteral(nextString());
            ATermAppl r = nextTerm();

            kb.addIndividual(x);
            kb.addIndividual(y);
            kb.addPropertyValue(r, x, y);

            if (log.isDebugEnabled())
                log.debug("DATATYPE-ROLE-FILLER " + x + " - " + r + " -> " + y);
        } else
            throw new RuntimeException("Unknown command " + str);
        skipNext(')');

        token = in.nextToken();
    }
}

From source file:org.mindswap.pellet.KRSSLoader.java

public void verifyTBox(String file, KnowledgeBase kb) throws Exception {
    initTokenizer(new FileReader(file));

    int verifiedCount = 0;
    int token = in.nextToken();
    while (token != ')' && token != StreamTokenizer.TT_EOF) {
        ATermUtils.assertTrue(token == '(');

        verifiedCount++;//w  ww  . j a  v  a 2s. co m

        ATermAppl c = null;
        if (peekNext('(')) {
            ATermAppl[] list = parseExprList();
            c = list[0];
            Set eqs = kb.getEquivalentClasses(c);
            for (int i = 1; i < list.length; i++) {
                ATermAppl t = list[i];

                if (!eqs.contains(t))
                    throw new RuntimeException(t + " is not equivalent to " + c);
            }
        } else
            c = parseExpr();

        Set supers = SetUtils.union(kb.getSuperClasses(c, true));
        Set subs = SetUtils.union(kb.getSubClasses(c, true));

        if (log.isDebugEnabled())
            log.debug("Verify (" + verifiedCount + ") " + c + " " + supers + " " + subs);

        if (peekNext('(')) {
            ATermAppl[] terms = parseExprList();
            for (int i = 0; i < terms.length; i++) {
                ATerm t = terms[i];

                if (!supers.contains(t))
                    throw new RuntimeException(t + " is not a superclass of " + c + " " + supers);
            }
        } else
            skipNext();

        if (peekNext('(')) {
            ATermAppl[] terms = parseExprList();
            for (int i = 0; i < terms.length; i++) {
                ATermAppl t = terms[i];

                if (!subs.contains(t)) {
                    Set temp = new HashSet(subs);
                    Set sames = kb.getEquivalentClasses(t);
                    temp.retainAll(sames);
                    if (temp.size() == 0)
                        throw new RuntimeException(t + " is not a subclass of " + c + " " + subs);
                }
            }
        }

        skipNext();

        token = in.nextToken();
    }

    ATermUtils.assertTrue(in.nextToken() == StreamTokenizer.TT_EOF);
}

From source file:org.mindswap.pellet.KRSSLoader.java

public void verifyABox(String file, KnowledgeBase kb) throws Exception {
    initTokenizer(new FileReader(file));

    boolean longFormat = !peekNext('(');

    while (!peekNext(StreamTokenizer.TT_EOF)) {
        if (longFormat) {
            skipNext("Command");
            skipNext('=');
        }/*from   w ww.  j a  v a  2s . com*/

        skipNext('(');
        skipNext("INDIVIDUAL-INSTANCE?");

        ATermAppl ind = nextTerm();
        ATermAppl c = parseExpr();

        if (log.isDebugEnabled())
            log.debug("INDIVIDUAL-INSTANCE? " + ind + " " + c);

        skipNext(')');

        boolean isType;
        if (longFormat) {
            skipNext('-');
            skipNext('>');
            String result = nextString();
            if (result.equalsIgnoreCase("T"))
                isType = true;
            else if (result.equalsIgnoreCase("NIL"))
                isType = false;
            else
                throw new RuntimeException("Unknown result " + result);
        } else
            isType = true;

        if (log.isDebugEnabled())
            log.debug(" -> " + isType);

        if (kb.isType(ind, c) != isType) {
            throw new RuntimeException(
                    "Individual " + ind + " is " + (isType ? "not" : "") + " an instance of " + c);
        }
    }
}

From source file:org.opencms.main.CmsShell.java

/**
 * Executes all commands read from the given input stream.<p>
 * /*from w  w  w  .  j a  v a 2  s. co  m*/
 * @param fileInputStream a file input stream from which the commands are read
 */
private void executeCommands(FileInputStream fileInputStream) {

    try {
        LineNumberReader lnr = new LineNumberReader(new InputStreamReader(fileInputStream));
        while (!m_exitCalled) {
            printPrompt();
            String line = lnr.readLine();
            if (line == null) {
                // if null the file has been read to the end
                try {
                    Thread.sleep(500);
                } catch (Throwable t) {
                    // noop
                }
                break;
            }
            if (line.trim().startsWith("#")) {
                System.out.println(line);
                continue;
            }
            StringReader reader = new StringReader(line);
            StreamTokenizer st = new StreamTokenizer(reader);
            st.eolIsSignificant(true);

            // put all tokens into a List
            List<String> parameters = new ArrayList<String>();
            while (st.nextToken() != StreamTokenizer.TT_EOF) {
                if (st.ttype == StreamTokenizer.TT_NUMBER) {
                    parameters.add(Integer.toString(new Double(st.nval).intValue()));
                } else {
                    parameters.add(st.sval);
                }
            }
            reader.close();

            // extract command and arguments
            if (parameters.size() == 0) {
                if (m_echo) {
                    System.out.println();
                }
                continue;
            }
            String command = parameters.get(0);
            parameters = parameters.subList(1, parameters.size());

            // execute the command
            executeCommand(command, parameters);
        }
    } catch (Throwable t) {
        t.printStackTrace(System.err);
    }
}

From source file:org.openhab.io.caldav.internal.util.ExecuteCommandJob.java

/**
 * Parses a <code>command</code>. Utilizes the {@link StreamTokenizer} which
 * takes care of quoted Strings as well.
 * //from ww  w  . j ava  2  s  .  c om
 * @param command the command to parse 
 * @return the tokenized command which can be processed by the 
 * <code>ConsoleInterpreter</code>
 * 
 * @see org.openhab.io.console.ConsoleInterpreter
 */
protected String[] parseCommand(String command) {
    logger.trace("going to parse command '{}'", command);

    // if the command starts with '>' it contains a script which needs no
    // further handling here ...
    if (command.startsWith(">")) {
        return new String[] { ">", command.substring(1).trim() };
    }

    StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(command));
    tokenizer.wordChars('_', '_');
    tokenizer.wordChars('-', '-');
    tokenizer.wordChars('.', '.');

    List<String> tokens = new ArrayList<String>();
    try {
        int tokenType = 0;
        while (tokenType != StreamTokenizer.TT_EOF && tokenType != StreamTokenizer.TT_EOL) {
            tokenType = tokenizer.nextToken();
            String token = "";
            switch (tokenType) {
            case StreamTokenizer.TT_WORD:
            case 34 /* quoted String */:
                token = tokenizer.sval;
                break;
            case StreamTokenizer.TT_NUMBER:
                token = String.valueOf(tokenizer.nval);
                break;
            }
            tokens.add(token);
            logger.trace("read value {} from the given command", token);
        }
    } catch (IOException ioe) {
    }

    return tokens.toArray(new String[0]);
}

From source file:org.openmrs.cohort.CohortSearchHistory.java

public PatientSearch createCompositionFilter(String description) {
    Set<String> andWords = new HashSet<String>();
    Set<String> orWords = new HashSet<String>();
    Set<String> notWords = new HashSet<String>();
    andWords.add("and");
    andWords.add("intersection");
    andWords.add("*");
    orWords.add("or");
    orWords.add("union");
    orWords.add("+");
    notWords.add("not");
    notWords.add("!");

    List<Object> currentLine = new ArrayList<Object>();

    try {// ww w. java 2s .c  o m
        StreamTokenizer st = new StreamTokenizer(new StringReader(description));
        st.ordinaryChar('(');
        st.ordinaryChar(')');
        Stack<List<Object>> stack = new Stack<List<Object>>();
        while (st.nextToken() != StreamTokenizer.TT_EOF) {
            if (st.ttype == StreamTokenizer.TT_NUMBER) {
                Integer thisInt = new Integer((int) st.nval);
                if (thisInt < 1 || thisInt > searchHistory.size()) {
                    log.error("number < 1 or > search history size");
                    return null;
                }
                currentLine.add(thisInt);
            } else if (st.ttype == '(') {
                stack.push(currentLine);
                currentLine = new ArrayList<Object>();
            } else if (st.ttype == ')') {
                List<Object> l = stack.pop();
                l.add(currentLine);
                currentLine = l;
            } else if (st.ttype == StreamTokenizer.TT_WORD) {
                String str = st.sval.toLowerCase();
                if (andWords.contains(str))
                    currentLine.add(PatientSetService.BooleanOperator.AND);
                else if (orWords.contains(str))
                    currentLine.add(PatientSetService.BooleanOperator.OR);
                else if (notWords.contains(str))
                    currentLine.add(PatientSetService.BooleanOperator.NOT);
                else
                    throw new IllegalArgumentException("Don't recognize " + st.sval);
            }
        }
    } catch (Exception ex) {
        log.error("Error in description string: " + description, ex);
        return null;
    }

    if (!testCompositionList(currentLine)) {
        log.error("Description string failed test: " + description);
        return null;
    }

    //return toPatientFilter(currentLine);
    PatientSearch ret = new PatientSearch();
    ret.setParsedComposition(currentLine);
    return ret;
}

From source file:org.openmrs.module.reporting.cohort.definition.util.CohortExpressionParser.java

/**
 * Elements in this list can be: an Integer, indicating a 1-based index into a search history a
 * BooleanOperator (AND, OR, NOT) a CohortDefinition a PatientSearch another List of the same form,
 * which indicates a parenthetical expression
 *///from w  w w.  j a  v  a2 s  .  c  o m
public static List<Object> parseIntoTokens(String expression) {

    List<Object> tokens = new ArrayList<Object>();
    try {
        StreamTokenizer st = new StreamTokenizer(new StringReader(expression));
        for (Character c : characterWords) {
            st.ordinaryChar(c);
        }
        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 (openParenthesesWords.contains(Character.valueOf((char) st.ttype))) {
                tokens.add("(");
            } else if (closeParenthesesWords.contains(Character.valueOf((char) st.ttype))) {
                tokens.add(")");
            } else if (st.ttype == StreamTokenizer.TT_WORD) {
                tokens.add(st.sval);
            }
        }
        return parseIntoTokens(tokens);
    } catch (Exception ex) {
        log.error("Error in description string: " + expression, ex);
        return null;
    }
}

From source file:org.openmrs.module.reporting.query.evaluator.CompositionQueryEvaluator.java

/**
 * Elements in this list can be: an Integer, indicating a 1-based index into a search history a
 * BooleanOperator (AND, OR, NOT) a Query, another List of the same form, which indicates a parenthetical expression
 *//*ww w  .  j  a  v  a2s  .c om*/
public List<Object> parseIntoTokens(String expression) throws EvaluationException {

    List<Object> tokens = new ArrayList<Object>();
    try {
        StreamTokenizer st = new StreamTokenizer(new StringReader(expression));
        for (Character c : CHARACTER_WORDS) {
            st.ordinaryChar(c);
        }
        while (st.nextToken() != StreamTokenizer.TT_EOF) {
            if (st.ttype == StreamTokenizer.TT_NUMBER) {
                Integer thisInt = (int) st.nval;
                if (thisInt < 1) {
                    throw new IllegalArgumentException("Invalid number < 1 found");
                }
                tokens.add(thisInt);
            } else if (OPEN_PARENTHESES_WORDS.contains(Character.valueOf((char) st.ttype))) {
                tokens.add("(");
            } else if (CLOSE_PARENTHESES_WORDS.contains(Character.valueOf((char) st.ttype))) {
                tokens.add(")");
            } else if (st.ttype == StreamTokenizer.TT_WORD) {
                tokens.add(st.sval);
            }
        }
        return parseIntoTokens(tokens);
    } catch (Exception e) {
        throw new EvaluationException("Unable to parse expression <" + expression + "> into tokens", e);
    }
}

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  .j  a 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;
    }
}