[OLINGO-63] Uri Parser: Add support for II

This commit is contained in:
Sven Kobler 2013-12-18 11:23:48 +01:00
parent 021afffb50
commit cf4df9be8a
16 changed files with 106 additions and 72 deletions

View File

@ -19,19 +19,19 @@
package org.apache.olingo.odata4.producer.core.uri; package org.apache.olingo.odata4.producer.core.uri;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import org.apache.olingo.odata4.producer.api.uri.UriInfoKind; 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.expression.Expression;
import org.apache.olingo.odata4.producer.core.uri.queryoption.Filter;
public class UriInfoImplPath extends UriInfoImpl { public class UriInfoImplPath extends UriInfoImpl {
private List<UriPathInfoImpl> pathInfos = new ArrayList<UriPathInfoImpl>(); private List<UriPathInfoImpl> pathInfos = new ArrayList<UriPathInfoImpl>();
private Expression spFilter;
//TODO add other systemQueryParamters
private Filter spFilter;
public UriInfoImplPath() { public UriInfoImplPath() {
this.setKind(UriInfoKind.path); this.setKind(UriInfoKind.path);
@ -54,11 +54,11 @@ public class UriInfoImplPath extends UriInfoImpl {
} }
public void setSystemParameter(SystemQueryParameter filter, Expression expression) { public void setSystemParameter(SystemQueryParameter filter, Expression expression) {
spFilter = expression; spFilter = new Filter(expression);
addQueryParameter(filter.toString(), expression); addQueryParameter(filter.toString(), expression);
} }
public Expression getFilter() { public Filter getFilter() {
return this.spFilter; return this.spFilter;
} }
@ -72,15 +72,9 @@ public class UriInfoImplPath extends UriInfoImpl {
} }
ret += pathInfos.get(i).toString(); ret += pathInfos.get(i).toString();
i++; i++;
} }
return ret; return ret;
} }

View File

@ -21,8 +21,6 @@ package org.apache.olingo.odata4.producer.core.uri;
import java.util.List; import java.util.List;
import javax.annotation.processing.SupportedAnnotationTypes;
import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.ParseTree;
import org.apache.olingo.odata4.commons.api.edm.Edm; import org.apache.olingo.odata4.commons.api.edm.Edm;
import org.apache.olingo.odata4.commons.api.edm.EdmAction; 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.SkiptokenContext;
import org.apache.olingo.odata4.producer.core.uri.antlr.UriParserParser.SystemQueryOptionContext; 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.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 { public class UriParserImpl {
private Edm edm = null; private Edm edm = null;
@ -158,7 +165,7 @@ public class UriParserImpl {
} else if (firstChild instanceof CustomQueryOptionContext) { } else if (firstChild instanceof CustomQueryOptionContext) {
// TODO read custom request option // TODO read custom request option
} else if (firstChild.getText().equals("@")) { } 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) { private Expression readBinary(ParseTree expressionContext) {
BinaryOperator expression = new BinaryOperator(); Binary expression = new Binary();
expression.setLeftOperand(readCommonExpression(expressionContext.getChild(0))); expression.setLeftOperand(readCommonExpression(expressionContext.getChild(0)));
expression.setOperator(SupportedBinaryOperators.get(expressionContext.getChild(2).getText())); expression.setOperator(SupportedBinaryOperators.get(expressionContext.getChild(2).getText()));
expression.setRightOperand(readCommonExpression(expressionContext.getChild(4))); expression.setRightOperand(readCommonExpression(expressionContext.getChild(4)));

View File

@ -22,16 +22,14 @@ package org.apache.olingo.odata4.producer.core.uri.expression;
public class Alias extends Expression { public class Alias extends Expression {
private String referenceName; private String referenceName;
//TODO add object which is referenced
public void setReference(String referenceName) { public void setReference(String referenceName) {
this.referenceName = referenceName; this.referenceName = referenceName;
} }
@Override @Override
public Object accept(ExpressionVisitor visitor) throws ExceptionVisitExpression { public <T> T accept(ExpressionVisitor<T> visitor) throws ExceptionVisitExpression {
// TODO Auto-generated method stub return visitor.visitAlias(referenceName);
return null;
} }
} }

View File

@ -18,13 +18,13 @@
******************************************************************************/ ******************************************************************************/
package org.apache.olingo.odata4.producer.core.uri.expression; 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 SupportedBinaryOperators operator;
private Expression left; private Expression left;
private Expression right; private Expression right;
public BinaryOperator setOperator(SupportedBinaryOperators operator) { public Binary setOperator(SupportedBinaryOperators operator) {
this.operator = operator; this.operator = operator;
return this; return this;
} }

View File

@ -20,4 +20,9 @@ package org.apache.olingo.odata4.producer.core.uri.expression;
public class ExceptionVisitExpression extends Exception { public class ExceptionVisitExpression extends Exception {
/**
*
*/
private static final long serialVersionUID = 822365726050299076L;
} }

View File

@ -26,9 +26,11 @@ public interface ExpressionVisitor<T> {
T visitUnaryOperator( SupportedUnaryOperators operator, T operand) throws ExceptionVisitExpression; 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 visitLiteral(String literal) throws ExceptionVisitExpression;
T visitMember(Member member) throws ExceptionVisitExpression;; T visitMember(Member member) throws ExceptionVisitExpression;
T visitAlias(String referenceName) throws ExceptionVisitExpression;
} }

View File

@ -24,7 +24,6 @@ public class Literal extends Expression implements Visitable {
public void setText(String text) { public void setText(String text) {
this.text = text; this.text = text;
} }
@Override @Override

View File

@ -22,16 +22,21 @@ import org.apache.olingo.odata4.producer.core.uri.UriInfoImplPath;
public class Member extends Expression implements Visitable { public class Member extends Expression implements Visitable {
private boolean isIT; private boolean isIT; // means $it as defined in the ABNF
UriInfoImplPath path; private UriInfoImplPath path;
public boolean isIT() {
return isIT;
}
public Member setIT(boolean isIT) { public Member setIT(boolean isIT) {
this.isIT = isIT; this.isIT = isIT;
return this; return this;
} }
public boolean isIT() {
return isIT; public UriInfoImplPath getPath() {
return path;
} }
public Member setPath(UriInfoImplPath pathSegments) { public Member setPath(UriInfoImplPath pathSegments) {
@ -39,13 +44,11 @@ public class Member extends Expression implements Visitable {
return this; return this;
} }
@Override @Override
public <T> T accept(ExpressionVisitor<T> visitor) throws ExceptionVisitExpression { public <T> T accept(ExpressionVisitor<T> visitor) throws ExceptionVisitExpression {
return visitor.visitMember(this); return visitor.visitMember(this);
} }
public UriInfoImplPath getPath() {
return path;
}
} }

View File

@ -35,9 +35,12 @@ public class MethodCall extends Expression implements Visitable{
} }
@Override @Override
public Object accept(ExpressionVisitor visitor) throws ExceptionVisitExpression { public <T> T accept(ExpressionVisitor<T> visitor) throws ExceptionVisitExpression {
List<T> userParameters = new ArrayList<T>();
return null; for (Expression parameter : parameters ) {
userParameters.add(parameter.accept(visitor));
}
return visitor.visitMethodCall(method, userParameters);
} }
} }

View File

@ -19,10 +19,17 @@
package org.apache.olingo.odata4.producer.core.uri.expression; package org.apache.olingo.odata4.producer.core.uri.expression;
public enum SupportedBinaryOperators { public enum SupportedBinaryOperators {
//multiplicative
MUL("mul"), DIV("div"), MOD("mod"), 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"), ISOF("isof"),
//equality
EQ("eq"), NE("ne"), EQ("eq"), NE("ne"),
//and/or
AND("and"), OR("or"); AND("and"), OR("or");
private String syntax; private String syntax;
@ -31,11 +38,6 @@ public enum SupportedBinaryOperators {
this.syntax = syntax; this.syntax = syntax;
} }
@Override
public String toString() {
return syntax;
}
public static SupportedBinaryOperators get(String operator) { public static SupportedBinaryOperators get(String operator) {
for (SupportedBinaryOperators op : SupportedBinaryOperators.values()) { for (SupportedBinaryOperators op : SupportedBinaryOperators.values()) {
if (op.toString().equals(operator)) { if (op.toString().equals(operator)) {
@ -45,4 +47,10 @@ public enum SupportedBinaryOperators {
return null; return null;
} }
@Override
public String toString() {
return syntax;
}
} }

View File

@ -25,18 +25,16 @@ public class UnaryOperator extends Expression implements Visitable {
public void setOperand(Expression expression) { public void setOperand(Expression expression) {
this.expression = expression; this.expression = expression;
} }
public void setOperator(SupportedUnaryOperators operator) { public void setOperator(SupportedUnaryOperators operator) {
this.operator = operator; this.operator = operator;
} }
@Override @Override
public Object accept(ExpressionVisitor visitor) throws ExceptionVisitExpression { public <T> T accept(ExpressionVisitor<T> visitor) throws ExceptionVisitExpression {
// TODO Auto-generated method stub T operand = expression.accept(visitor);
return null; return visitor.visitUnaryOperator(operator, operand);
} }
} }

View File

@ -18,7 +18,6 @@
******************************************************************************/ ******************************************************************************/
package org.apache.olingo.odata4.producer.core.uri.expression; package org.apache.olingo.odata4.producer.core.uri.expression;
import org.antlr.v4.runtime.tree.ParseTreeVisitor;

View File

@ -18,6 +18,19 @@
******************************************************************************/ ******************************************************************************/
package org.apache.olingo.odata4.producer.core.uri.queryoption; package org.apache.olingo.odata4.producer.core.uri.queryoption;
import org.apache.olingo.odata4.producer.core.uri.expression.Expression;
public class Filter extends SystemQueryOption { public class Filter extends SystemQueryOption {
Expression expression;
public Filter(Expression expression) {
this.expression = expression;
}
public Expression getTree() {
return expression;
}
} }

View File

@ -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.UriInfoImplPath;
import org.apache.olingo.odata4.producer.core.uri.expression.ExceptionVisitExpression; 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.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.Member;
import org.apache.olingo.odata4.producer.core.uri.expression.SupportedBinaryOperators; 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.SupportedMethodCalls;
@ -43,13 +42,13 @@ public class FilterTreeToText implements ExpressionVisitor<String> {
} }
@Override @Override
public String visitMethoCall(SupportedMethodCalls methodCall, List<String> parameters) public String visitMethodCall(SupportedMethodCalls methodCall, List<String> parameters)
throws ExceptionVisitExpression { throws ExceptionVisitExpression {
String text = "<" + methodCall + "("; String text = "<" + methodCall + "(";
int i = 0; int i = 0;
while (i< parameters.size()) { while (i < parameters.size()) {
if (i > 0 ) { if (i > 0) {
text +=","; text += ",";
} }
text += parameters.get(i); text += parameters.get(i);
i++; i++;
@ -58,10 +57,9 @@ public class FilterTreeToText implements ExpressionVisitor<String> {
return text + ")"; return text + ")";
} }
@Override @Override
public String visitLiteral(String literal) throws ExceptionVisitExpression { public String visitLiteral(String literal) throws ExceptionVisitExpression {
return "" + literal + ""; return literal;
} }
@Override @Override
@ -72,16 +70,22 @@ public class FilterTreeToText implements ExpressionVisitor<String> {
} }
UriInfoImplPath path = member.getPath(); UriInfoImplPath path = member.getPath();
if (path!= null) { if (path != null) {
/*if (member.isIT()) { /*
ret +="/"; * if (member.isIT()) {
}*/ * ret +="/";
* }
*/
ret += path.toString(); ret += path.toString();
} }
return ret; return ret;
} }
@Override
public String visitAlias(String referenceName) throws ExceptionVisitExpression {
return "<" + referenceName + ">";
}
} }

View File

@ -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.UriInfoImplPath;
import org.apache.olingo.odata4.producer.core.uri.expression.ExceptionVisitExpression; 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.expression.Expression;
import org.apache.olingo.odata4.producer.core.uri.queryoption.Filter;
public class FilterValidator { public class FilterValidator {
UriResourcePathValidator uriResourcePathValidator; UriResourcePathValidator uriResourcePathValidator;
Expression filterTree; Filter filter;
public FilterValidator(UriResourcePathValidator uriResourcePathValidator) { public FilterValidator(UriResourcePathValidator uriResourcePathValidator) {
this.uriResourcePathValidator = uriResourcePathValidator; this.uriResourcePathValidator = uriResourcePathValidator;
filterTree = ((UriInfoImplPath) uriResourcePathValidator.uriInfo).getFilter(); filter = ((UriInfoImplPath) uriResourcePathValidator.uriInfo).getFilter();
if (filterTree == null) { if (filter.getTree() == null) {
fail("FilterValidator: no filter found"); fail("FilterValidator: no filter found");
} }
return; return;
@ -49,7 +50,7 @@ public class FilterValidator {
public FilterValidator is(String expectedFilterAsString) { public FilterValidator is(String expectedFilterAsString) {
try { try {
String actualFilterAsText = filterTree.accept(new FilterTreeToText()); String actualFilterAsText = filter.getTree().accept(new FilterTreeToText());
assertEquals(expectedFilterAsString, actualFilterAsText); assertEquals(expectedFilterAsString, actualFilterAsText);
} catch (ExceptionVisitExpression e) { } catch (ExceptionVisitExpression e) {
fail("Exception occured while converting the filterTree into text" + "\n" fail("Exception occured while converting the filterTree into text" + "\n"

View File

@ -180,7 +180,7 @@ public class UriResourcePathValidator {
} }
public UriResourcePathValidator isFilterString(String expectedFilterTreeAsString) { public UriResourcePathValidator isFilterString(String expectedFilterTreeAsString) {
Expression filterTree = this.uriInfo.getFilter(); Expression filterTree = this.uriInfo.getFilter().getTree();
try { try {
String filterTreeAsString = filterTree.accept(new FilterTreeToText()); String filterTreeAsString = filterTree.accept(new FilterTreeToText());
assertEquals(expectedFilterTreeAsString, filterTreeAsString); assertEquals(expectedFilterTreeAsString, filterTreeAsString);