Search in sources :

Example 26 with Parser

use of org.apache.commons.jexl3.parser.Parser 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)

Example 27 with Parser

use of org.apache.commons.jexl3.parser.Parser in project if688.github.io by if688.

the class App method main.

public static void main(String[] args) throws NotLL1Exception, ErrorOnParseException {
    /*
		 * Gramática de exemplo:
		 *  A -> aB
		 * 	B -> cC
		 * 	C -> d
		 * 
		 */
    Nonterminal start = new Nonterminal("A");
    Nonterminal B = new Nonterminal("B");
    Nonterminal C = new Nonterminal("C");
    Terminal a = new Terminal("a");
    Terminal c = new Terminal("c");
    Terminal d = new Terminal("d");
    List<GeneralSymbol> prodA = new ArrayList<GeneralSymbol>();
    prodA.add(a);
    prodA.add(B);
    List<GeneralSymbol> prodB = new ArrayList<GeneralSymbol>();
    prodB.add(c);
    prodB.add(C);
    List<GeneralSymbol> prodC = new ArrayList<GeneralSymbol>();
    prodC.add(d);
    Production fpA = new Production(start, prodA);
    Production pB = new Production(B, prodB);
    Production pC = new Production(C, prodC);
    Collection<Production> col = new ArrayList<Production>();
    col.add(fpA);
    col.add(pB);
    col.add(pC);
    List<Terminal> example = new ArrayList<Terminal>();
    example.add(a);
    example.add(c);
    example.add(d);
    Grammar g = new Grammar(col, start);
    Map<Nonterminal, Set<GeneralSymbol>> first = SetGenerator.getFirst(g);
    Map<Nonterminal, Set<GeneralSymbol>> follow = SetGenerator.getFollow(g, first);
    Map<LL1Key, List<GeneralSymbol>> table = Table.createTable(g);
    Parser parser = ParserGenerator.createParser(g);
    ParseTree parseTree = ParserUtils.parseSequence(parser, example);
    System.out.println("Exemplo 1:\n" + "A -> aB\n" + "B -> cC\n" + "C -> d");
    System.out.println("Conjunto first: " + first.toString());
    System.out.println("Conjunto follow: " + follow.toString());
    System.out.println("Tabela de parsing: " + table.toString());
    System.out.println("Exemplo de parsing: " + parseTree.toString() + "\n");
}
Also used : Set(java.util.Set) ArrayList(java.util.ArrayList) Parser(br.ufpe.cin.if688.parser.Parser) LL1Key(br.ufpe.cin.if688.table.LL1Key) ArrayList(java.util.ArrayList) List(java.util.List) ParseTree(br.ufpe.cin.if688.parser.ParseTree)

Example 28 with Parser

use of org.apache.commons.jexl3.parser.Parser in project if688.github.io by if688.

the class PosFixa method main.

public static void main(String[] args) throws IOException {
    String expression = readFile(file, StandardCharsets.UTF_8);
    Lexer lexer = new Lexer(expression);
    // System.out.println(lexer.tokens());
    Parser parse = new Parser(lexer);
    parse.expr();
    System.out.write('\n');
/**/
}
Also used : Lexer(br.ufpe.cin.if688.infpos.v2.lexer.Lexer) Parser(br.ufpe.cin.if688.infpos.v2.parser.Parser)

Example 29 with Parser

use of org.apache.commons.jexl3.parser.Parser in project jetty.project by eclipse.

the class WindowUpdateGenerateParseTest method testGenerateParseOneByteAtATime.

@Test
public void testGenerateParseOneByteAtATime() throws Exception {
    WindowUpdateGenerator generator = new WindowUpdateGenerator(new HeaderGenerator());
    final List<WindowUpdateFrame> frames = new ArrayList<>();
    Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

        @Override
        public void onWindowUpdate(WindowUpdateFrame frame) {
            frames.add(frame);
        }
    }, 4096, 8192);
    int streamId = 13;
    int windowUpdate = 17;
    // Iterate a few times to be sure generator and parser are properly reset.
    for (int i = 0; i < 2; ++i) {
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        generator.generateWindowUpdate(lease, streamId, windowUpdate);
        frames.clear();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            while (buffer.hasRemaining()) {
                parser.parse(ByteBuffer.wrap(new byte[] { buffer.get() }));
            }
        }
        Assert.assertEquals(1, frames.size());
        WindowUpdateFrame frame = frames.get(0);
        Assert.assertEquals(streamId, frame.getStreamId());
        Assert.assertEquals(windowUpdate, frame.getWindowDelta());
    }
}
Also used : MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) WindowUpdateGenerator(org.eclipse.jetty.http2.generator.WindowUpdateGenerator) HeaderGenerator(org.eclipse.jetty.http2.generator.HeaderGenerator) Test(org.junit.Test)

Example 30 with Parser

use of org.apache.commons.jexl3.parser.Parser in project jetty.project by eclipse.

the class WindowUpdateGenerateParseTest method testGenerateParse.

@Test
public void testGenerateParse() throws Exception {
    WindowUpdateGenerator generator = new WindowUpdateGenerator(new HeaderGenerator());
    final List<WindowUpdateFrame> frames = new ArrayList<>();
    Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

        @Override
        public void onWindowUpdate(WindowUpdateFrame frame) {
            frames.add(frame);
        }
    }, 4096, 8192);
    int streamId = 13;
    int windowUpdate = 17;
    // Iterate a few times to be sure generator and parser are properly reset.
    for (int i = 0; i < 2; ++i) {
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        generator.generateWindowUpdate(lease, streamId, windowUpdate);
        frames.clear();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            while (buffer.hasRemaining()) {
                parser.parse(buffer);
            }
        }
    }
    Assert.assertEquals(1, frames.size());
    WindowUpdateFrame frame = frames.get(0);
    Assert.assertEquals(streamId, frame.getStreamId());
    Assert.assertEquals(windowUpdate, frame.getWindowDelta());
}
Also used : MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) WindowUpdateGenerator(org.eclipse.jetty.http2.generator.WindowUpdateGenerator) HeaderGenerator(org.eclipse.jetty.http2.generator.HeaderGenerator) Test(org.junit.Test)

Aggregations

Parser (org.eclipse.jetty.http2.parser.Parser)37 ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)37 ByteBuffer (java.nio.ByteBuffer)36 Test (org.junit.Test)34 MappedByteBufferPool (org.eclipse.jetty.io.MappedByteBufferPool)25 ArrayList (java.util.ArrayList)22 HeaderGenerator (org.eclipse.jetty.http2.generator.HeaderGenerator)21 HttpFields (org.eclipse.jetty.http.HttpFields)17 MetaData (org.eclipse.jetty.http.MetaData)17 HashMap (java.util.HashMap)15 OutputStream (java.io.OutputStream)13 Socket (java.net.Socket)13 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)13 PrefaceFrame (org.eclipse.jetty.http2.frames.PrefaceFrame)13 SettingsFrame (org.eclipse.jetty.http2.frames.SettingsFrame)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 HttpServlet (javax.servlet.http.HttpServlet)7 HostPortHttpField (org.eclipse.jetty.http.HostPortHttpField)6