mirror of
https://github.com/apache/olingo-odata4.git
synced 2025-03-06 16:49:09 +00:00
[OLINGO-63] Uri Parser: Add support for II
This commit is contained in:
parent
021afffb50
commit
cf4df9be8a
@ -19,19 +19,19 @@
|
||||
package org.apache.olingo.odata4.producer.core.uri;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.olingo.odata4.producer.api.uri.UriInfoKind;
|
||||
import org.apache.olingo.odata4.producer.core.uri.expression.Expression;
|
||||
import org.apache.olingo.odata4.producer.core.uri.queryoption.Filter;
|
||||
|
||||
public class UriInfoImplPath extends UriInfoImpl {
|
||||
|
||||
private List<UriPathInfoImpl> pathInfos = new ArrayList<UriPathInfoImpl>();
|
||||
|
||||
private Expression spFilter;
|
||||
|
||||
//TODO add other systemQueryParamters
|
||||
private Filter spFilter;
|
||||
|
||||
public UriInfoImplPath() {
|
||||
this.setKind(UriInfoKind.path);
|
||||
@ -54,11 +54,11 @@ public class UriInfoImplPath extends UriInfoImpl {
|
||||
}
|
||||
|
||||
public void setSystemParameter(SystemQueryParameter filter, Expression expression) {
|
||||
spFilter = expression;
|
||||
spFilter = new Filter(expression);
|
||||
addQueryParameter(filter.toString(), expression);
|
||||
}
|
||||
|
||||
public Expression getFilter() {
|
||||
public Filter getFilter() {
|
||||
return this.spFilter;
|
||||
}
|
||||
|
||||
@ -72,15 +72,9 @@ public class UriInfoImplPath extends UriInfoImpl {
|
||||
}
|
||||
|
||||
ret += pathInfos.get(i).toString();
|
||||
|
||||
|
||||
|
||||
i++;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -21,8 +21,6 @@ package org.apache.olingo.odata4.producer.core.uri;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.processing.SupportedAnnotationTypes;
|
||||
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.apache.olingo.odata4.commons.api.edm.Edm;
|
||||
import org.apache.olingo.odata4.commons.api.edm.EdmAction;
|
||||
@ -82,7 +80,16 @@ import org.apache.olingo.odata4.producer.core.uri.antlr.UriParserParser.SkipCont
|
||||
import org.apache.olingo.odata4.producer.core.uri.antlr.UriParserParser.SkiptokenContext;
|
||||
import org.apache.olingo.odata4.producer.core.uri.antlr.UriParserParser.SystemQueryOptionContext;
|
||||
import org.apache.olingo.odata4.producer.core.uri.antlr.UriParserParser.TopContext;
|
||||
import org.apache.olingo.odata4.producer.core.uri.expression.*;
|
||||
import org.apache.olingo.odata4.producer.core.uri.expression.Alias;
|
||||
import org.apache.olingo.odata4.producer.core.uri.expression.Binary;
|
||||
import org.apache.olingo.odata4.producer.core.uri.expression.Expression;
|
||||
import org.apache.olingo.odata4.producer.core.uri.expression.Literal;
|
||||
import org.apache.olingo.odata4.producer.core.uri.expression.Member;
|
||||
import org.apache.olingo.odata4.producer.core.uri.expression.MethodCall;
|
||||
import org.apache.olingo.odata4.producer.core.uri.expression.SupportedBinaryOperators;
|
||||
import org.apache.olingo.odata4.producer.core.uri.expression.SupportedMethodCalls;
|
||||
import org.apache.olingo.odata4.producer.core.uri.expression.SupportedUnaryOperators;
|
||||
import org.apache.olingo.odata4.producer.core.uri.expression.UnaryOperator;
|
||||
|
||||
public class UriParserImpl {
|
||||
private Edm edm = null;
|
||||
@ -158,7 +165,7 @@ public class UriParserImpl {
|
||||
} else if (firstChild instanceof CustomQueryOptionContext) {
|
||||
// TODO read custom request option
|
||||
} else if (firstChild.getText().equals("@")) {
|
||||
// TODO read ailas and value
|
||||
// TODO read alias and value
|
||||
}
|
||||
|
||||
}
|
||||
@ -274,7 +281,7 @@ public class UriParserImpl {
|
||||
}
|
||||
|
||||
private Expression readBinary(ParseTree expressionContext) {
|
||||
BinaryOperator expression = new BinaryOperator();
|
||||
Binary expression = new Binary();
|
||||
expression.setLeftOperand(readCommonExpression(expressionContext.getChild(0)));
|
||||
expression.setOperator(SupportedBinaryOperators.get(expressionContext.getChild(2).getText()));
|
||||
expression.setRightOperand(readCommonExpression(expressionContext.getChild(4)));
|
||||
|
@ -22,16 +22,14 @@ package org.apache.olingo.odata4.producer.core.uri.expression;
|
||||
public class Alias extends Expression {
|
||||
|
||||
private String referenceName;
|
||||
//TODO add object which is referenced
|
||||
|
||||
public void setReference(String referenceName) {
|
||||
this.referenceName = referenceName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object accept(ExpressionVisitor visitor) throws ExceptionVisitExpression {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
public <T> T accept(ExpressionVisitor<T> visitor) throws ExceptionVisitExpression {
|
||||
return visitor.visitAlias(referenceName);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,13 +18,13 @@
|
||||
******************************************************************************/
|
||||
package org.apache.olingo.odata4.producer.core.uri.expression;
|
||||
|
||||
public class BinaryOperator extends Expression implements Visitable {
|
||||
public class Binary extends Expression implements Visitable {
|
||||
|
||||
private SupportedBinaryOperators operator;
|
||||
private Expression left;
|
||||
private Expression right;
|
||||
|
||||
public BinaryOperator setOperator(SupportedBinaryOperators operator) {
|
||||
public Binary setOperator(SupportedBinaryOperators operator) {
|
||||
this.operator = operator;
|
||||
return this;
|
||||
}
|
@ -20,4 +20,9 @@ package org.apache.olingo.odata4.producer.core.uri.expression;
|
||||
|
||||
public class ExceptionVisitExpression extends Exception {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 822365726050299076L;
|
||||
|
||||
}
|
||||
|
@ -26,9 +26,11 @@ public interface ExpressionVisitor<T> {
|
||||
|
||||
T visitUnaryOperator( SupportedUnaryOperators operator, T operand) throws ExceptionVisitExpression;
|
||||
|
||||
T visitMethoCall( SupportedMethodCalls methodCall, List<T> parameters)throws ExceptionVisitExpression;
|
||||
T visitMethodCall( SupportedMethodCalls methodCall, List<T> parameters)throws ExceptionVisitExpression;
|
||||
|
||||
T visitLiteral(String literal) throws ExceptionVisitExpression;
|
||||
|
||||
T visitMember(Member member) throws ExceptionVisitExpression;;
|
||||
T visitMember(Member member) throws ExceptionVisitExpression;
|
||||
|
||||
T visitAlias(String referenceName) throws ExceptionVisitExpression;
|
||||
}
|
||||
|
@ -24,7 +24,6 @@ public class Literal extends Expression implements Visitable {
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -22,16 +22,21 @@ import org.apache.olingo.odata4.producer.core.uri.UriInfoImplPath;
|
||||
|
||||
public class Member extends Expression implements Visitable {
|
||||
|
||||
private boolean isIT;
|
||||
UriInfoImplPath path;
|
||||
private boolean isIT; // means $it as defined in the ABNF
|
||||
private UriInfoImplPath path;
|
||||
|
||||
public boolean isIT() {
|
||||
return isIT;
|
||||
}
|
||||
|
||||
public Member setIT(boolean isIT) {
|
||||
this.isIT = isIT;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isIT() {
|
||||
return isIT;
|
||||
|
||||
public UriInfoImplPath getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
public Member setPath(UriInfoImplPath pathSegments) {
|
||||
@ -39,13 +44,11 @@ public class Member extends Expression implements Visitable {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public <T> T accept(ExpressionVisitor<T> visitor) throws ExceptionVisitExpression {
|
||||
return visitor.visitMember(this);
|
||||
|
||||
}
|
||||
|
||||
public UriInfoImplPath getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -35,9 +35,12 @@ public class MethodCall extends Expression implements Visitable{
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object accept(ExpressionVisitor visitor) throws ExceptionVisitExpression {
|
||||
|
||||
return null;
|
||||
public <T> T accept(ExpressionVisitor<T> visitor) throws ExceptionVisitExpression {
|
||||
List<T> userParameters = new ArrayList<T>();
|
||||
for (Expression parameter : parameters ) {
|
||||
userParameters.add(parameter.accept(visitor));
|
||||
}
|
||||
return visitor.visitMethodCall(method, userParameters);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -19,10 +19,17 @@
|
||||
package org.apache.olingo.odata4.producer.core.uri.expression;
|
||||
|
||||
public enum SupportedBinaryOperators {
|
||||
//multiplicative
|
||||
MUL("mul"), DIV("div"), MOD("mod"),
|
||||
ADD("add"), SUB("sub"), GT("gt"), GE("ge"), LT("lt"), LE("le"),
|
||||
//additive
|
||||
ADD("add"), SUB("sub"),
|
||||
//comparism
|
||||
GT("gt"), GE("ge"), LT("lt"), LE("le"),
|
||||
//isof
|
||||
ISOF("isof"),
|
||||
//equality
|
||||
EQ("eq"), NE("ne"),
|
||||
//and/or
|
||||
AND("and"), OR("or");
|
||||
|
||||
private String syntax;
|
||||
@ -31,11 +38,6 @@ public enum SupportedBinaryOperators {
|
||||
this.syntax = syntax;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return syntax;
|
||||
}
|
||||
|
||||
public static SupportedBinaryOperators get(String operator) {
|
||||
for (SupportedBinaryOperators op : SupportedBinaryOperators.values()) {
|
||||
if (op.toString().equals(operator)) {
|
||||
@ -45,4 +47,10 @@ public enum SupportedBinaryOperators {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return syntax;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -25,18 +25,16 @@ public class UnaryOperator extends Expression implements Visitable {
|
||||
|
||||
public void setOperand(Expression expression) {
|
||||
this.expression = expression;
|
||||
|
||||
}
|
||||
|
||||
public void setOperator(SupportedUnaryOperators operator) {
|
||||
this.operator = operator;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object accept(ExpressionVisitor visitor) throws ExceptionVisitExpression {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
public <T> T accept(ExpressionVisitor<T> visitor) throws ExceptionVisitExpression {
|
||||
T operand = expression.accept(visitor);
|
||||
return visitor.visitUnaryOperator(operator, operand);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,7 +18,6 @@
|
||||
******************************************************************************/
|
||||
package org.apache.olingo.odata4.producer.core.uri.expression;
|
||||
|
||||
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
|
||||
|
||||
|
||||
|
||||
|
@ -18,6 +18,19 @@
|
||||
******************************************************************************/
|
||||
package org.apache.olingo.odata4.producer.core.uri.queryoption;
|
||||
|
||||
import org.apache.olingo.odata4.producer.core.uri.expression.Expression;
|
||||
|
||||
public class Filter extends SystemQueryOption {
|
||||
|
||||
|
||||
Expression expression;
|
||||
|
||||
public Filter(Expression expression) {
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
public Expression getTree() {
|
||||
return expression;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -23,7 +23,6 @@ import java.util.List;
|
||||
import org.apache.olingo.odata4.producer.core.uri.UriInfoImplPath;
|
||||
import org.apache.olingo.odata4.producer.core.uri.expression.ExceptionVisitExpression;
|
||||
import org.apache.olingo.odata4.producer.core.uri.expression.ExpressionVisitor;
|
||||
import org.apache.olingo.odata4.producer.core.uri.expression.Literal;
|
||||
import org.apache.olingo.odata4.producer.core.uri.expression.Member;
|
||||
import org.apache.olingo.odata4.producer.core.uri.expression.SupportedBinaryOperators;
|
||||
import org.apache.olingo.odata4.producer.core.uri.expression.SupportedMethodCalls;
|
||||
@ -43,13 +42,13 @@ public class FilterTreeToText implements ExpressionVisitor<String> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitMethoCall(SupportedMethodCalls methodCall, List<String> parameters)
|
||||
public String visitMethodCall(SupportedMethodCalls methodCall, List<String> parameters)
|
||||
throws ExceptionVisitExpression {
|
||||
String text = "<" + methodCall + "(";
|
||||
int i = 0;
|
||||
while (i< parameters.size()) {
|
||||
if (i > 0 ) {
|
||||
text +=",";
|
||||
while (i < parameters.size()) {
|
||||
if (i > 0) {
|
||||
text += ",";
|
||||
}
|
||||
text += parameters.get(i);
|
||||
i++;
|
||||
@ -58,10 +57,9 @@ public class FilterTreeToText implements ExpressionVisitor<String> {
|
||||
return text + ")";
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String visitLiteral(String literal) throws ExceptionVisitExpression {
|
||||
return "" + literal + "";
|
||||
return literal;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -72,16 +70,22 @@ public class FilterTreeToText implements ExpressionVisitor<String> {
|
||||
}
|
||||
|
||||
UriInfoImplPath path = member.getPath();
|
||||
if (path!= null) {
|
||||
/*if (member.isIT()) {
|
||||
ret +="/";
|
||||
}*/
|
||||
if (path != null) {
|
||||
/*
|
||||
* if (member.isIT()) {
|
||||
* ret +="/";
|
||||
* }
|
||||
*/
|
||||
|
||||
ret += path.toString();
|
||||
|
||||
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitAlias(String referenceName) throws ExceptionVisitExpression {
|
||||
return "<" + referenceName + ">";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -24,18 +24,19 @@ import static org.junit.Assert.fail;
|
||||
import org.apache.olingo.odata4.producer.core.uri.UriInfoImplPath;
|
||||
import org.apache.olingo.odata4.producer.core.uri.expression.ExceptionVisitExpression;
|
||||
import org.apache.olingo.odata4.producer.core.uri.expression.Expression;
|
||||
import org.apache.olingo.odata4.producer.core.uri.queryoption.Filter;
|
||||
|
||||
public class FilterValidator {
|
||||
|
||||
UriResourcePathValidator uriResourcePathValidator;
|
||||
Expression filterTree;
|
||||
Filter filter;
|
||||
|
||||
public FilterValidator(UriResourcePathValidator uriResourcePathValidator) {
|
||||
|
||||
this.uriResourcePathValidator = uriResourcePathValidator;
|
||||
|
||||
filterTree = ((UriInfoImplPath) uriResourcePathValidator.uriInfo).getFilter();
|
||||
if (filterTree == null) {
|
||||
filter = ((UriInfoImplPath) uriResourcePathValidator.uriInfo).getFilter();
|
||||
if (filter.getTree() == null) {
|
||||
fail("FilterValidator: no filter found");
|
||||
}
|
||||
return;
|
||||
@ -49,7 +50,7 @@ public class FilterValidator {
|
||||
|
||||
public FilterValidator is(String expectedFilterAsString) {
|
||||
try {
|
||||
String actualFilterAsText = filterTree.accept(new FilterTreeToText());
|
||||
String actualFilterAsText = filter.getTree().accept(new FilterTreeToText());
|
||||
assertEquals(expectedFilterAsString, actualFilterAsText);
|
||||
} catch (ExceptionVisitExpression e) {
|
||||
fail("Exception occured while converting the filterTree into text" + "\n"
|
||||
|
@ -180,7 +180,7 @@ public class UriResourcePathValidator {
|
||||
}
|
||||
|
||||
public UriResourcePathValidator isFilterString(String expectedFilterTreeAsString) {
|
||||
Expression filterTree = this.uriInfo.getFilter();
|
||||
Expression filterTree = this.uriInfo.getFilter().getTree();
|
||||
try {
|
||||
String filterTreeAsString = filterTree.accept(new FilterTreeToText());
|
||||
assertEquals(expectedFilterTreeAsString, filterTreeAsString);
|
||||
|
Loading…
x
Reference in New Issue
Block a user