Search in sources :

Example 46 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project nikita-noark5-core by HiOA-ABI.

the class TestODataApp method main.

public static void main(String[] args) throws Exception {
    System.out.println("Starting OData parser test");
    System.out.println("Picks first line from odata_samples.txt in " + "resources folder.");
    try {
        AfterApplicationStartup afterApplicationStartup = new AfterApplicationStartup(null);
        afterApplicationStartup.populateTranslatedNames();
        TestODataApp app = new TestODataApp();
        ODataLexer lexer = new ODataLexer(CharStreams.fromStream(app.getInputStreamForParseFile("odata" + File.separator + "odata_samples.txt")));
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        ODataParser parser = new ODataParser(tokens);
        ParseTree tree = parser.odataURL();
        ParseTreeWalker walker = new ParseTreeWalker();
        // Make the SQL Statement
        NikitaODataToSQLWalker sqlWalker = new NikitaODataToSQLWalker();
        walker.walk(sqlWalker, tree);
        System.out.println(sqlWalker.getSqlStatement());
    } catch (RecognitionException e) {
        throw new IllegalStateException("Recognition exception");
    }
}
Also used : ODataParser(nikita.webapp.odata.ODataParser) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ODataLexer(nikita.webapp.odata.ODataLexer) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) NikitaODataToSQLWalker(nikita.webapp.odata.NikitaODataToSQLWalker) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 47 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project nikita-noark5-core by HiOA-ABI.

the class OdataTest method testOdata.

@RequestMapping(method = RequestMethod.GET, value = "arkivstruktur/{\\w*}")
public ResponseEntity<String> testOdata(final UriComponentsBuilder uriBuilder, HttpServletRequest request, final HttpServletResponse response) throws Exception {
    String uqueryString = request.getQueryString();
    String decoded = URLDecoder.decode(uqueryString, UTF_8);
    StringBuffer originalRequest = request.getRequestURL();
    originalRequest.append("?" + decoded);
    CharStream stream = CharStreams.fromString(originalRequest.toString());
    ODataLexer lexer = new ODataLexer(stream);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ODataParser parser = new ODataParser(tokens);
    ParseTree tree = parser.odataURL();
    ParseTreeWalker walker = new ParseTreeWalker();
    // Make the HQL Statement
    NikitaODataToHQLWalker hqlWalker = new NikitaODataToHQLWalker();
    walker.walk(hqlWalker, tree);
    Session session = entityManager.unwrap(org.hibernate.Session.class);
    Query query = hqlWalker.getHqlStatment(session);
    String queryString = query.getQueryString();
    System.out.println(queryString);
    List<NoarkEntity> list = query.getResultList();
    return ResponseEntity.status(HttpStatus.CREATED).body(list.toString());
}
Also used : ODataParser(nikita.webapp.odata.ODataParser) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Query(org.hibernate.query.Query) NoarkEntity(nikita.common.model.noark5.v4.NoarkEntity) CharStream(org.antlr.v4.runtime.CharStream) ODataLexer(nikita.webapp.odata.ODataLexer) NikitaODataToHQLWalker(nikita.webapp.odata.NikitaODataToHQLWalker) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) Session(org.hibernate.Session) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 48 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project oxTrust by GluuFederation.

the class ScimFilterParserService method walkTree.

private void walkTree(String filter, ScimFilterBaseListener listener) throws SCIMException {
    ScimFilterErrorListener errorListener = new ScimFilterErrorListener();
    ParseTree tree = getParseTree(filter, errorListener);
    checkParsingErrors(errorListener);
    ParseTreeWalker.DEFAULT.walk(listener, tree);
}
Also used : ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 49 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project oxTrust by GluuFederation.

the class ScimFilterParserService method getParseTree.

public ParseTree getParseTree(String filter) throws Exception {
    ScimFilterErrorListener errorListener = new ScimFilterErrorListener();
    ParseTree tree = getParseTree(filter, errorListener);
    checkParsingErrors(errorListener);
    return tree;
}
Also used : ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 50 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project oxTrust by GluuFederation.

the class Scim2PatchService method applyPatchOperationWithValueFilter.

private BaseScimResource applyPatchOperationWithValueFilter(BaseScimResource resource, PatchOperation operation, String valSelFilter, String attribute, String subAttribute) throws SCIMException, InvalidAttributeValueException {
    String path = operation.getPath();
    ObjectMapper mapper = new ObjectMapper();
    Class<? extends BaseScimResource> cls = resource.getClass();
    Map<String, Object> resourceAsMap = mapper.convertValue(resource, new TypeReference<Map<String, Object>>() {
    });
    List<Map<String, Object>> list;
    Attribute attrAnnot = IntrospectUtil.getFieldAnnotation(attribute, cls, Attribute.class);
    if (attrAnnot != null) {
        if (!attrAnnot.multiValueClass().equals(NullType.class) && attrAnnot.type().equals(AttributeDefinition.Type.COMPLEX)) {
            Object colObject = resourceAsMap.get(attribute);
            list = colObject == null ? null : new ArrayList<Map<String, Object>>((Collection<Map<String, Object>>) colObject);
        } else
            throw new SCIMException(String.format("Attribute '%s' expected to be complex multi-valued", attribute));
    } else
        throw new SCIMException(String.format("Attribute '%s' not recognized or expected to be complex multi-valued", attribute));
    if (list == null)
        log.info("applyPatchOperationWithValueFilter. List of values for {} is empty. Operation has no effect", attribute);
    else {
        try {
            valSelFilter = FilterUtil.preprocess(valSelFilter, cls);
            ParseTree parseTree = filterService.getParseTree(valSelFilter);
            List<Integer> matchingIndexes = new ArrayList<Integer>();
            for (int i = 0; i < list.size(); i++) {
                if (filterService.complexAttributeMatch(parseTree, list.get(i), attribute, cls))
                    // Important: add so that resulting list is reverse-ordered
                    matchingIndexes.add(0, i);
            }
            if (subAttribute.length() > 0 && matchingIndexes.size() > 0 && operation.getType().equals(PatchOperationType.REMOVE)) {
                // per spec (section 3.5.2.2 RFC 7644) subAttribute must not be required or read-only
                Attribute subAttrAnnot = IntrospectUtil.getFieldAnnotation(attribute + "." + subAttribute, cls, Attribute.class);
                if (subAttrAnnot != null && (subAttrAnnot.mutability().equals(READ_ONLY) || subAttrAnnot.isRequired()))
                    throw new InvalidAttributeValueException("Cannot remove read-only or required attribute " + attribute + "." + subAttribute);
            }
            /*
                Here we differ from spec (see section 3.5.2.3/4 of RFC7644. If no record match is made, we are supposed to
                return error 400 with scimType of noTarget. But this is clearly inconvenient
                */
            log.info("There are {} entries matching the filter '{}'", matchingIndexes.size(), path);
            for (Integer index : matchingIndexes) {
                if (operation.getType().equals(PatchOperationType.REMOVE)) {
                    if (// Remove the whole item
                    subAttribute.length() == 0)
                        // If intValue is not used, the remove(Object) method is called!
                        list.remove(index.intValue());
                    else
                        // remove subattribute only
                        list.get(index).remove(subAttribute);
                } else
                    applyPartialUpdate(attribute, subAttribute, list, index, operation.getValue(), cls);
            }
            log.trace("New {} list is:\n{}", attribute, mapper.writeValueAsString(list));
            resourceAsMap.put(attribute, list.size() == 0 ? null : list);
            resource = mapper.convertValue(resourceAsMap, cls);
        } catch (InvalidAttributeValueException ei) {
            throw ei;
        } catch (Exception e) {
            log.info("Error processing Patch operation with value selection path '{}'", path);
            log.error(e.getMessage(), e);
            throw new SCIMException(e.getMessage(), e);
        }
    }
    return resource;
}
Also used : Attribute(org.gluu.oxtrust.model.scim2.annotations.Attribute) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) SCIMException(org.gluu.oxtrust.model.exception.SCIMException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) SCIMException(org.gluu.oxtrust.model.exception.SCIMException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Aggregations

ParseTree (org.antlr.v4.runtime.tree.ParseTree)311 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)104 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)60 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)49 AddedParseTree (org.eclipse.titan.common.parsers.AddedParseTree)46 CharStream (org.antlr.v4.runtime.CharStream)43 Test (org.junit.Test)43 CommonToken (org.antlr.v4.runtime.CommonToken)35 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)35 JavadocContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.JavadocContext)31 TextContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.TextContext)29 File (java.io.File)26 ArrayList (java.util.ArrayList)22 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)22 CancellationException (java.util.concurrent.CancellationException)20 ConsoleErrorListener (org.antlr.v4.runtime.ConsoleErrorListener)20 Grammar (org.antlr.v4.tool.Grammar)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 IOException (java.io.IOException)19 LexerGrammar (org.antlr.v4.tool.LexerGrammar)16