Search in sources :

Example 1 with Token

use of org.apache.commons.jexl3.parser.Token in project graal by graphik-team.

the class DlgpParseException method createMsg.

private static final String createMsg(fr.lirmm.graphik.dlgp2.parser.ParseException e) {
    Token tok = e.currentToken;
    if (e.currentToken != null) {
        String extract = "";
        do {
            if (tok.kind == 0) {
                extract += e.tokenImage[0];
                break;
            }
            extract += add_escapes(tok.image);
            tok = tok.next;
        } while (tok != null);
        return "a parse exception occured on \"" + extract + "\"";
    } else {
        return e.getMessage();
    }
}
Also used : Token(fr.lirmm.graphik.dlgp2.parser.Token)

Example 2 with Token

use of org.apache.commons.jexl3.parser.Token in project syncope by apache.

the class AbstractAnyDAO method getWhereClause.

/**
 * Generate one where clause for each different attribute schema into the derived schema expression provided.
 *
 * @param expression derived schema expression
 * @param value derived attribute value
 * @param attrUtils USER / GROUP
 * @return where clauses to use to build the query
 */
private Set<String> getWhereClause(final String expression, final String value) {
    Parser parser = new Parser(new StringReader(expression));
    // Schema names
    List<String> identifiers = new ArrayList<>();
    // Literals
    List<String> literals = new ArrayList<>();
    // Get schema names and literals
    for (Token token = parser.getNextToken(); token != null && StringUtils.isNotBlank(token.toString()); token = parser.getNextToken()) {
        if (token.kind == ParserConstants.STRING_LITERAL) {
            literals.add(token.toString().substring(1, token.toString().length() - 1));
        }
        if (token.kind == ParserConstants.IDENTIFIER) {
            identifiers.add(token.toString());
        }
    }
    // Sort literals in order to process later literals included into others
    Collections.sort(literals, (final String t, final String t1) -> {
        if (t == null && t1 == null) {
            return 0;
        } else if (t != null && t1 == null) {
            return -1;
        } else if (t == null && t1 != null) {
            return 1;
        } else if (t.length() == t1.length()) {
            return 0;
        } else if (t.length() > t1.length()) {
            return -1;
        } else {
            return 1;
        }
    });
    // Split value on provided literals
    List<String> attrValues = split(value, literals);
    if (attrValues.size() != identifiers.size()) {
        LOG.error("Ambiguous JEXL expression resolution: literals and values have different size");
        return Collections.emptySet();
    }
    // clauses to be used with INTERSECTed queries
    Set<String> clauses = new HashSet<>();
    // builder to build the clauses
    StringBuilder bld = new StringBuilder();
    // Contains used identifiers in order to avoid replications
    Set<String> used = new HashSet<>();
    // Create several clauses: one for eanch identifiers
    for (int i = 0; i < identifiers.size(); i++) {
        if (!used.contains(identifiers.get(i))) {
            // verify schema existence and get schema type
            PlainSchema schema = plainSchemaDAO().find(identifiers.get(i));
            if (schema == null) {
                LOG.error("Invalid schema '{}', ignoring", identifiers.get(i));
            } else {
                // clear builder
                bld.delete(0, bld.length());
                bld.append("(");
                // set schema name
                bld.append("s.id = '").append(identifiers.get(i)).append("'");
                bld.append(" AND ");
                bld.append("s.id = a.schema_id").append(" AND ");
                bld.append("a.id = v.attribute_id");
                bld.append(" AND ");
                // use a value clause different for eanch different schema type
                switch(schema.getType()) {
                    case Boolean:
                        bld.append("v.booleanValue = '").append(attrValues.get(i)).append("'");
                        break;
                    case Long:
                        bld.append("v.longValue = ").append(attrValues.get(i));
                        break;
                    case Double:
                        bld.append("v.doubleValue = ").append(attrValues.get(i));
                        break;
                    case Date:
                        bld.append("v.dateValue = '").append(attrValues.get(i)).append("'");
                        break;
                    default:
                        bld.append("v.stringValue = '").append(attrValues.get(i)).append("'");
                }
                bld.append(")");
                used.add(identifiers.get(i));
                clauses.add(bld.toString());
            }
        }
    }
    LOG.debug("Generated where clauses {}", clauses);
    return clauses;
}
Also used : StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) Token(org.apache.commons.jexl3.parser.Token) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) Parser(org.apache.commons.jexl3.parser.Parser) HashSet(java.util.HashSet)

Aggregations

Token (fr.lirmm.graphik.dlgp2.parser.Token)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Parser (org.apache.commons.jexl3.parser.Parser)1 Token (org.apache.commons.jexl3.parser.Token)1 PlainSchema (org.apache.syncope.core.persistence.api.entity.PlainSchema)1