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");
}
}
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());
}
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);
}
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;
}
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;
}
Aggregations