org.apache.olingo.server.api.uri.queryoption.expression.Expression Java Examples

The following examples show how to use org.apache.olingo.server.api.uri.queryoption.expression.Expression. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExpressionParser.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private Expression parseExprMul() throws UriParserException, UriValidationException {
  Expression left = parseExprUnary();
  TokenKind operatorTokenKind = ParserHelper.next(tokenizer,
      TokenKind.MulOperator, TokenKind.DivOperator, TokenKind.ModOperator);
  // Null for everything other than MUL or DIV or MOD
  while (operatorTokenKind != null) {
    checkNumericType(left);
    final Expression right = parseExprUnary();
    checkNumericType(right);
    left = new BinaryImpl(left, tokenToBinaryOperator.get(operatorTokenKind), right,
        odata.createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Double));
    operatorTokenKind = ParserHelper.next(tokenizer,
        TokenKind.MulOperator, TokenKind.DivOperator, TokenKind.ModOperator);
  }
  return left;
}
 
Example #2
Source File: ExpressionParserTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void relational() throws Exception {
  Expression expression = parseExpression("5 gt 5");
  assertEquals("{5 GT 5}", expression.toString());

  expression = parseExpression("5 ge 5");
  assertEquals("{5 GE 5}", expression.toString());

  expression = parseExpression("5 lt 5");
  assertEquals("{5 LT 5}", expression.toString());

  expression = parseExpression("5 le 5");
  assertEquals("{5 LE 5}", expression.toString());

  assertEquals("{5 LE 5.1}", parseExpression("5 le 5.1").toString());

  assertEquals("{1 GT null}", parseExpression("1 gt null").toString());
  assertEquals("{null GE 2}", parseExpression("null ge 2").toString());
  assertEquals("{null LE null}", parseExpression("null le null").toString());

  wrongExpression("5 gt duration'PT5H'");
}
 
Example #3
Source File: ExpressionParser.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void checkEqualityTypes(final Expression left, final Expression right) throws UriParserException {
  checkNoCollection(left);
  checkNoCollection(right);

  final EdmType leftType = getType(left);
  final EdmType rightType = getType(right);
  if (leftType == null || rightType == null || leftType.equals(rightType)) {
    return;
  }

  // Numeric promotion for Edm.Byte and Edm.SByte
  if (isType(leftType, EdmPrimitiveTypeKind.Byte, EdmPrimitiveTypeKind.SByte)
      && isType(rightType, EdmPrimitiveTypeKind.Byte, EdmPrimitiveTypeKind.SByte)) {
    return;
  }

  if (leftType.getKind() != EdmTypeKind.PRIMITIVE
      || rightType.getKind() != EdmTypeKind.PRIMITIVE
      || !(((EdmPrimitiveType) leftType).isCompatible((EdmPrimitiveType) rightType)
      || ((EdmPrimitiveType) rightType).isCompatible((EdmPrimitiveType) leftType))) {
    throw new UriParserSemanticException("Incompatible types.",
        UriParserSemanticException.MessageKeys.TYPES_NOT_COMPATIBLE,
        leftType.getFullQualifiedName().getFullQualifiedNameAsString(),
        rightType.getFullQualifiedName().getFullQualifiedNameAsString());
  }
}
 
Example #4
Source File: ApplyParser.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private Compute parseComputeTrafo(EdmStructuredType referencedType)
    throws UriParserException, UriValidationException {
  ComputeImpl compute = new ComputeImpl();
  do {
    final Expression expression = new ExpressionParser(edm, odata)
        .parse(tokenizer, referencedType, crossjoinEntitySetNames, aliases);
    final EdmType expressionType = ExpressionParser.getType(expression);
    if (expressionType.getKind() != EdmTypeKind.PRIMITIVE) {
      throw new UriParserSemanticException("Compute expressions must return primitive values.",
          UriParserSemanticException.MessageKeys.ONLY_FOR_PRIMITIVE_TYPES, "compute");
    }
    final String alias = parseAsAlias(referencedType, true);
    ((DynamicStructuredType) referencedType).addProperty(createDynamicProperty(alias, expressionType));
    compute.addExpression(new ComputeExpressionImpl()
        .setExpression(expression)
        .setAlias(alias));
  } while (tokenizer.next(TokenKind.COMMA));
  ParserHelper.requireNext(tokenizer, TokenKind.CLOSE);
  return compute;
}
 
Example #5
Source File: FilterValidator.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public FilterValidator isParameterText(final int parameterIndex, final String parameterText)
    throws ExpressionVisitException, ODataApplicationException {

  if (curExpression instanceof MethodImpl) {
    MethodImpl methodCall = (MethodImpl) curExpression;

    Expression parameter = methodCall.getParameters().get(parameterIndex);
    String actualParameterText = FilterTreeToText.Serialize(parameter);
    assertEquals(parameterText, actualParameterText);
  } else if (curExpression instanceof MemberImpl) {
    final MemberImpl member = (MemberImpl) curExpression;
    final List<UriResource> uriResourceParts = member.getResourcePath().getUriResourceParts();

    if (!uriResourceParts.isEmpty() && uriResourceParts.get(0) instanceof UriResourceFunctionImpl) {
      assertEquals(parameterText, ((UriResourceFunctionImpl) uriResourceParts.get(0)).getParameters()
          .get(parameterIndex).getText());
    } else {
      fail("Current expression is not a method or function");
    }
  } else {
    fail("Current expression is not a method or function");
  }

  return this;
}
 
Example #6
Source File: ExpressionParserTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void parseMethodWithParametersWithoutAlias(TokenKind kind, String... parameters) 
    throws UriParserException, UriValidationException {
  final String methodName = kind.name().substring(0, kind.name().indexOf("Method")).toLowerCase(Locale.ROOT)
      .replace("geo", "geo.");
  String expressionString = methodName + '(';
  expressionString += "@word1";
  expressionString += ',';
  expressionString += parameters[1];
  expressionString += ')';
  expressionString += "&@word1=" + parameters[0];

  Map<String, AliasQueryOption> alias = new HashMap<String, AliasQueryOption>();
  AliasQueryOptionImpl aliasQueryOption = new AliasQueryOptionImpl();
  aliasQueryOption.setName("@word");
  aliasQueryOption.setText("\'a\'");
  alias.put("@word", aliasQueryOption);
  UriTokenizer tokenizer = new UriTokenizer(expressionString);
  final Expression expression = new ExpressionParser(mock(Edm.class), odata).parse(tokenizer, null, null, alias);
  assertNotNull(expression);
  
  assertEquals('{' + methodName + ' ' + "[@word1, " + parameters[1] + ']' + '}',
      expression.toString());
  
}
 
Example #7
Source File: ExpressionParserTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void testIntegerTypes() throws Exception {
  Expression expression = parseExpression("5 ne 545678979");
  assertEquals("{5 NE 545678979}", expression.toString());
  
  expression = parseExpression("5456 eq 5456");
  assertEquals("{5456 EQ 5456}", expression.toString());
  
  expression = parseExpression("null ne 54561234567");
  assertEquals("{null NE 54561234567}", expression.toString());
  
  expression = parseExpression("null ne 255");
  assertEquals("{null NE 255}", expression.toString());
  
  expression = parseExpression("123 le 2551234567890000999999");
  assertEquals("{123 LE 2551234567890000999999}", expression.toString());
}
 
Example #8
Source File: ExpressionParser.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
/**
 * @param expressionList
 * @throws UriParserException
 * @throws UriValidationException
 */
private List<Expression> parseInExpr() throws UriParserException, UriValidationException {
  List<Expression> expressionList = new ArrayList<>();
  while(!tokenizer.next(TokenKind.CLOSE)) {
    Expression expression = parseExpression();
    expressionList.add(expression);
    ParserHelper.bws(tokenizer);
    while (tokenizer.next(TokenKind.COMMA)) {
      ParserHelper.bws(tokenizer);
      expression = parseExpression();
      expressionList.add(expression);
      ParserHelper.bws(tokenizer);
    }
  }
  return expressionList;
}
 
Example #9
Source File: ExpressionTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void binaryExpression() throws Exception {
  Expression expressionLeft = new LiteralImpl("2", odata.createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Byte));
  Expression expressionRight = new LiteralImpl("-1", odata.createPrimitiveTypeInstance(EdmPrimitiveTypeKind.SByte));

  BinaryImpl expression = new BinaryImpl(expressionLeft, BinaryOperatorKind.SUB, expressionRight,
      odata.createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Byte));

  assertEquals(expressionLeft, expression.getLeftOperand());
  assertEquals(expressionRight, expression.getRightOperand());
  assertEquals(BinaryOperatorKind.SUB, expression.getOperator());
  assertEquals(odata.createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Byte), expression.getType());

  String output = expression.accept(new FilterTreeToText());
  assertEquals("<<2> sub <-1>>", output);
}
 
Example #10
Source File: ExpressionParser.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private Expression parseIsOfOrCastMethod(final MethodKind kind) throws UriParserException, UriValidationException {
  // The TokenKind 'IsOfMethod' consumes also the opening parenthesis.
  // The first parameter could be an expression or a type literal.
  List<Expression> parameters = new ArrayList<>();
  ParserHelper.bws(tokenizer);
  parameters.add(parseExpression());
  if (!(parameters.get(0) instanceof TypeLiteral)) {
    // The first parameter is not a type literal, so there must be a second parameter.
    ParserHelper.bws(tokenizer);
    ParserHelper.requireNext(tokenizer, TokenKind.COMMA);
    ParserHelper.bws(tokenizer);
    parameters.add(parseExpression());
    ParserHelper.bws(tokenizer);
    // The second parameter must be a type literal.
    if (!(parameters.get(1) instanceof TypeLiteral)) {
      throw new UriParserSemanticException("Type literal expected.",
          UriParserSemanticException.MessageKeys.INCOMPATIBLE_TYPE_FILTER);
    }
  }

  ParserHelper.requireNext(tokenizer, TokenKind.CLOSE);
  return new MethodImpl(kind, parameters);
}
 
Example #11
Source File: ExpressionParserTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void parseMethodWithParametersWithAlias(TokenKind kind, 
    String... parameters) throws UriParserException, UriValidationException {
  final String methodName = kind.name().substring(0, kind.name().indexOf("Method")).toLowerCase(Locale.ROOT)
      .replace("geo", "geo.");
  String expressionString = methodName + '(';
  expressionString += "@word";
  expressionString += ',';
  expressionString += parameters[1];
  expressionString += ')';
  expressionString += "&@word=" + parameters[0];

  Map<String, AliasQueryOption> alias = new HashMap<String, AliasQueryOption>();
  AliasQueryOptionImpl aliasQueryOption = new AliasQueryOptionImpl();
  aliasQueryOption.setName("@word");
  aliasQueryOption.setText("\'a\'");
  alias.put("@word", aliasQueryOption);
  UriTokenizer tokenizer = new UriTokenizer(expressionString);
  final Expression expression = new ExpressionParser(mock(Edm.class), odata).parse(tokenizer, null, null, alias);
  assertNotNull(expression);
  
  assertEquals('{' + methodName + ' ' + "[@word, " + parameters[1] + ']' + '}',
      expression.toString());
  
}
 
Example #12
Source File: ExpressionParserTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void parseMethod(TokenKind kind, String... parameters) throws UriParserException, UriValidationException {
  final String methodName = kind.name().substring(0, kind.name().indexOf("Method")).toLowerCase(Locale.ROOT)
      .replace("geo", "geo.");
  String expressionString = methodName + '(';
  boolean first = true;
  for (final String parameter : parameters) {
    if (first) {
      first = false;
    } else {
      expressionString += ',';
    }
    expressionString += parameter;
  }
  expressionString += ')';

  final Expression expression = parseExpression(expressionString);
  assertEquals('{' + methodName + ' ' + Arrays.toString(parameters) + '}',
      expression.toString());
}
 
Example #13
Source File: ExpressionParser.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private Expression parseExprRel() throws UriParserException, UriValidationException {
  if (tokenizer.next(TokenKind.IsofMethod)) {
    // The isof method is a terminal.  So no further operators are allowed.
    return parseIsOfOrCastMethod(MethodKind.ISOF);
  } else {
    Expression left = parseExprAdd();
    TokenKind operatorTokenKind = ParserHelper.next(tokenizer,
        TokenKind.GreaterThanOperator, TokenKind.GreaterThanOrEqualsOperator,
        TokenKind.LessThanOperator, TokenKind.LessThanOrEqualsOperator);
    // Null for everything other than GT or GE or LT or LE
    while (operatorTokenKind != null) {
      final Expression right = parseExprAdd();
      checkRelationTypes(left, right);
      left = new BinaryImpl(left, tokenToBinaryOperator.get(operatorTokenKind), right,
          odata.createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Boolean));
      operatorTokenKind = ParserHelper.next(tokenizer,
          TokenKind.GreaterThanOperator, TokenKind.GreaterThanOrEqualsOperator,
          TokenKind.LessThanOperator, TokenKind.LessThanOrEqualsOperator);
    }
    return left;
  }
}
 
Example #14
Source File: ExpressionParser.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private Expression parsePrimitive(final TokenKind primitiveTokenKind) throws UriParserException {
  final String primitiveValueLiteral = tokenizer.getText();
  if (primitiveTokenKind == TokenKind.EnumValue) {
    return createEnumExpression(primitiveValueLiteral);
  } else {
    EdmPrimitiveTypeKind primitiveTypeKind = ParserHelper.tokenToPrimitiveType.get(primitiveTokenKind);
    if (primitiveTypeKind == EdmPrimitiveTypeKind.Int64) {
      primitiveTypeKind = determineIntegerType(primitiveValueLiteral);
    }

    final EdmPrimitiveType type = primitiveTypeKind == null ?
        // Null handling
        null :
        odata.createPrimitiveTypeInstance(primitiveTypeKind);
    return new LiteralImpl(primitiveValueLiteral, type);
  }
}
 
Example #15
Source File: ExpressionParser.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private Expression parseExprEquality() throws UriParserException, UriValidationException {
  Expression left = parseExprRel();
  TokenKind operatorTokenKind = ParserHelper.next(tokenizer, TokenKind.EqualsOperator, TokenKind.NotEqualsOperator);
  // Null for everything other than EQ or NE
  while (operatorTokenKind != null) {
    final Expression right = parseExprEquality();
    checkEqualityTypes(left, right);
    left = new BinaryImpl(left, tokenToBinaryOperator.get(operatorTokenKind), right,
        odata.createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Boolean));
    operatorTokenKind = ParserHelper.next(tokenizer, TokenKind.EqualsOperator, TokenKind.NotEqualsOperator);
  }
  return left;
}
 
Example #16
Source File: ParserHelper.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
protected static AliasQueryOption parseAliasValue(final String name, final EdmType type, final boolean isNullable,
    final boolean isCollection, final Edm edm, final EdmType referringType,
    final Map<String, AliasQueryOption> aliases) throws UriParserException, UriValidationException {
  final EdmTypeKind kind = type == null ? null : type.getKind();
  final AliasQueryOption alias = aliases.get(name);
  if (alias != null && alias.getText() != null) {
    UriTokenizer aliasTokenizer = new UriTokenizer(alias.getText());
    if (kind == null
        || !((isCollection || kind == EdmTypeKind.COMPLEX || kind == EdmTypeKind.ENTITY ?
        aliasTokenizer.next(TokenKind.jsonArrayOrObject) :
        nextPrimitiveTypeValue(aliasTokenizer, (EdmPrimitiveType) type, isNullable))
        && aliasTokenizer.next(TokenKind.EOF))) {
      // The alias value is not an allowed literal value, so parse it again as expression.
      aliasTokenizer = new UriTokenizer(alias.getText());
      // Don't pass on the current alias to avoid circular references.
      Map<String, AliasQueryOption> aliasesInner = new HashMap<>(aliases);
      aliasesInner.remove(name);
      final Expression expression = new ExpressionParser(edm, odata)
          .parse(aliasTokenizer, referringType, null, aliasesInner);
      final EdmType expressionType = ExpressionParser.getType(expression);
      if (aliasTokenizer.next(TokenKind.EOF)
          && (expressionType == null || type == null || expressionType.equals(type))) {
        ((AliasQueryOptionImpl) alias).setAliasValue(expression);
      } else {
        throw new UriParserSemanticException("Illegal value for alias '" + alias.getName() + "'.",
            UriParserSemanticException.MessageKeys.UNKNOWN_PART, alias.getText());
      }
    }
  }
  return alias;
}
 
Example #17
Source File: ExpressionParser.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private Expression parseAnd() throws UriParserException, UriValidationException {
  Expression left = parseExprEquality();
  while (tokenizer.next(TokenKind.AndOperator)) {
    checkType(left, EdmPrimitiveTypeKind.Boolean);
    checkNoCollection(left);
    final Expression right = parseExprEquality();
    checkType(right, EdmPrimitiveTypeKind.Boolean);
    checkNoCollection(right);
    left = new BinaryImpl(left, BinaryOperatorKind.AND, right,
        odata.createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Boolean));
  }
  return left;
}
 
Example #18
Source File: ExpressionParser.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private Expression parseExpression() throws UriParserException, UriValidationException {
  Expression left = parseAnd();
  while (tokenizer.next(TokenKind.OrOperator)) {
    checkType(left, EdmPrimitiveTypeKind.Boolean);
    checkNoCollection(left);
    final Expression right = parseAnd();
    checkType(right, EdmPrimitiveTypeKind.Boolean);
    checkNoCollection(right);
    left = new BinaryImpl(left, BinaryOperatorKind.OR, right,
        odata.createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Boolean));
  }
  return left;
}
 
Example #19
Source File: FilterParser.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public FilterOption parse(UriTokenizer tokenizer, final EdmType referencedType,
    final Collection<String> crossjoinEntitySetNames, final Map<String, AliasQueryOption> aliases)
    throws UriParserException, UriValidationException {
  final Expression filterExpression = new ExpressionParser(edm, odata)
      .parse(tokenizer, referencedType, crossjoinEntitySetNames, aliases);
  final EdmType type = ExpressionParser.getType(filterExpression);
  if (type == null || type.equals(odata.createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Boolean))) {
    return new FilterOptionImpl().setExpression(filterExpression);
  } else {
    throw new UriParserSemanticException("Filter expressions must be boolean.",
        UriParserSemanticException.MessageKeys.TYPES_NOT_COMPATIBLE,
        "Edm.Boolean", type.getFullQualifiedName().getFullQualifiedNameAsString());
  }
}
 
Example #20
Source File: DemoEntityCollectionProcessor.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private List<Entity> applyFilterQueryOption(List<Entity> entityList, FilterOption filterOption)
    throws ODataApplicationException {

  if (filterOption != null) {
    try {
      Iterator<Entity> entityIterator = entityList.iterator();

      // Evaluate the expression for each entity
      // If the expression is evaluated to "true", keep the entity otherwise remove it from the entityList
      while (entityIterator.hasNext()) {
        // To evaluate the the expression, create an instance of the Filter Expression Visitor and pass
        // the current entity to the constructor
        Entity currentEntity = entityIterator.next();
        Expression filterExpression = filterOption.getExpression();
        FilterExpressionVisitor expressionVisitor = new FilterExpressionVisitor(currentEntity);

        // Start evaluating the expression
        Object visitorResult = filterExpression.accept(expressionVisitor);

        // The result of the filter expression must be of type Edm.Boolean
        if (visitorResult instanceof Boolean) {
          if (!Boolean.TRUE.equals(visitorResult)) {
            // The expression evaluated to false (or null), so we have to remove the currentEntity from entityList
            entityIterator.remove();
          }
        } else {
          throw new ODataApplicationException("A filter expression must evaulate to type Edm.Boolean",
              HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ENGLISH);
        }
      }

    } catch (ExpressionVisitException e) {
      throw new ODataApplicationException("Exception in filter evaluation",
          HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.ENGLISH);
    }
  }
  
  return entityList;
}
 
Example #21
Source File: ExpressionParserTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void testComplexPropertyPathExp() throws Exception {
  final String entitySetName = "ESName";
  final String keyPropertyName = "a";
  final String complexPropertyName = "comp";
  final String propertyName = "prop";
  EdmProperty keyProperty = mockProperty(keyPropertyName, 
      OData.newInstance().createPrimitiveTypeInstance(EdmPrimitiveTypeKind.String));
  EdmKeyPropertyRef keyPropertyRef = mockKeyPropertyRef(keyPropertyName, keyProperty);
  EdmProperty property = mockProperty(propertyName, 
      OData.newInstance().createPrimitiveTypeInstance(EdmPrimitiveTypeKind.String));
  
  EdmComplexType complexType = mockComplexType(propertyName, property);
  EdmProperty complexProperty = mockProperty(complexPropertyName, complexType);
  
  EdmEntityType entityType = mockEntityType(keyPropertyName, keyPropertyRef);
  Mockito.when(entityType.getPropertyNames()).thenReturn(Arrays.asList(keyPropertyName, complexPropertyName));
  Mockito.when(entityType.getProperty(keyPropertyName)).thenReturn(keyProperty);
  Mockito.when(entityType.getProperty(complexPropertyName)).thenReturn(complexProperty);
  EdmEntitySet entitySet = mockEntitySet(entitySetName, entityType);
  EdmEntityContainer container = mockContainer(entitySetName, entitySet);
  Edm mockedEdm = Mockito.mock(Edm.class);
  Mockito.when(mockedEdm.getEntityContainer()).thenReturn(container);
  
  UriTokenizer tokenizer = new UriTokenizer("comp/prop eq \'abc\'");
  Expression expression = new ExpressionParser(mockedEdm, odata).parse(tokenizer, 
      entityType, null, null);
  assertNotNull(expression);
  assertEquals("{[comp, prop] EQ \'abc\'}", expression.toString());
  
  tokenizer = new UriTokenizer("comp/prop in (\'abc\','xyz')");
  expression = new ExpressionParser(mockedEdm, odata).parse(tokenizer, 
      entityType, null, null);
  assertNotNull(expression);
  assertEquals("{[comp, prop] IN [\'abc\', \'xyz\']}", expression.toString());
}
 
Example #22
Source File: ExpressionParser.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
/**
 * @param expressionList
 * @param leftExprType
 * @throws UriParserException
 * @throws UriParserSemanticException
 */
private void checkInExpressionTypes(List<Expression> expressionList, EdmType leftExprType)
    throws UriParserException, UriParserSemanticException {
  for (Expression expr : expressionList) {
    EdmType inExprType = getType(expr);
    
    if (!(((EdmPrimitiveType) leftExprType).isCompatible((EdmPrimitiveType) inExprType))) {
      throw new UriParserSemanticException("Incompatible types.",
          UriParserSemanticException.MessageKeys.TYPES_NOT_COMPATIBLE,
          inExprType == null ? "" : inExprType.getFullQualifiedName().getFullQualifiedNameAsString(),
          leftExprType.getFullQualifiedName().getFullQualifiedNameAsString());
    }
  }
}
 
Example #23
Source File: ExpressionParserTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void multiplicative() throws Exception {
  Expression expression = parseExpression("5 mul 5");
  assertEquals("{5 MUL 5}", expression.toString());

  expression = parseExpression("5 div 5");
  assertEquals("{5 DIV 5}", expression.toString());

  expression = parseExpression("5 mod 5");
  assertEquals("{5 MOD 5}", expression.toString());

  wrongExpression("1 mod '2'");
}
 
Example #24
Source File: ExpressionParserTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void unary() throws Exception {
  Expression expression = parseExpression("-5");
  assertEquals("-5", expression.toString());

  assertEquals("{MINUS -1}", parseExpression("--1").toString());
  assertEquals("{MINUS duration'PT1M'}", parseExpression("-duration'PT1M'").toString());

  expression = parseExpression("not false");
  assertEquals("{NOT false}", expression.toString());

  wrongExpression("not 11:12:13");
}
 
Example #25
Source File: ExpressionParser.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
protected static EdmType getType(final Expression expression) throws UriParserException {
  EdmType type;
  if (expression instanceof Literal) {
    type = ((Literal) expression).getType();
  } else if (expression instanceof TypeLiteral) {
    type = ((TypeLiteral) expression).getType();
  } else if (expression instanceof Enumeration) {
    type = ((Enumeration) expression).getType();
  } else if (expression instanceof Member) {
    type = ((Member) expression).getType();
  } else if (expression instanceof Unary) {
    type = ((UnaryImpl) expression).getType();
  } else if (expression instanceof Binary) {
    type = ((BinaryImpl) expression).getType();
  } else if (expression instanceof Method) {
    type = ((MethodImpl) expression).getType();
  } else if (expression instanceof Alias) {
    final AliasQueryOption alias = ((AliasImpl) expression).getAlias();
    type = alias == null || alias.getValue() == null ? null : getType(alias.getValue());
  } else if (expression instanceof LambdaRef) {
    throw new UriParserSemanticException("Type determination not implemented.",
        UriParserSemanticException.MessageKeys.NOT_IMPLEMENTED, expression.toString());
  } else {
    throw new UriParserSemanticException("Unknown expression type.",
        UriParserSemanticException.MessageKeys.NOT_IMPLEMENTED, expression.toString());
  }
  if (type != null && type.getKind() == EdmTypeKind.DEFINITION) {
    type = ((EdmTypeDefinition) type).getUnderlyingType();
  }
  return type;
}
 
Example #26
Source File: ExpressionParser.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void checkType(final Expression expression, final EdmPrimitiveTypeKind... kinds) throws UriParserException {
  final EdmType type = getType(expression);
  if (!isType(type, kinds)) {
    throw new UriParserSemanticException("Incompatible types.",
        UriParserSemanticException.MessageKeys.TYPES_NOT_COMPATIBLE,
        type == null ? "" : type.getFullQualifiedName().getFullQualifiedNameAsString(),
        Arrays.deepToString(kinds));
  }
}
 
Example #27
Source File: ExpressionParserTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void equality() throws Exception {
  Expression expression = parseExpression("5 eq 5");
  assertEquals("{5 EQ 5}", expression.toString());

  expression = parseExpression("5 ne 5");
  assertEquals("{5 NE 5}", expression.toString());

  assertEquals("{1 EQ null}", parseExpression("1 eq null").toString());
  assertEquals("{null NE 2}", parseExpression("null ne 2").toString());
  assertEquals("{null EQ null}", parseExpression("null eq null").toString());

  wrongExpression("5 eq '5'");
  
}
 
Example #28
Source File: ApplyParser.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private BottomTop parseBottomTop(final TokenKind kind, final EdmStructuredType referencedType)
    throws UriParserException, UriValidationException {
  BottomTopImpl bottomTop = new BottomTopImpl();
  bottomTop.setMethod(TOKEN_KIND_TO_BOTTOM_TOP_METHOD.get(kind));
  final ExpressionParser expressionParser = new ExpressionParser(edm, odata);
  final Expression number = expressionParser.parse(tokenizer, referencedType, crossjoinEntitySetNames, aliases);
  expressionParser.checkIntegerType(number);
  bottomTop.setNumber(number);
  ParserHelper.requireNext(tokenizer, TokenKind.COMMA);
  final Expression value = expressionParser.parse(tokenizer, referencedType, crossjoinEntitySetNames, aliases);
  expressionParser.checkNumericType(value);
  bottomTop.setValue(value);
  ParserHelper.requireNext(tokenizer, TokenKind.CLOSE);
  return bottomTop;
}
 
Example #29
Source File: ExpressionParser.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private boolean isEnumType(final Expression expression) throws UriParserException {
  final EdmType expressionType = getType(expression);
  return expressionType == null
      || expressionType.getKind() == EdmTypeKind.ENUM
      || isType(expressionType,
          EdmPrimitiveTypeKind.Int16, EdmPrimitiveTypeKind.Int32, EdmPrimitiveTypeKind.Int64,
          EdmPrimitiveTypeKind.Byte, EdmPrimitiveTypeKind.SByte);
}
 
Example #30
Source File: ExpressionParser.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void checkRelationTypes(final Expression left, final Expression right) throws UriParserException {
  checkNoCollection(left);
  checkNoCollection(right);
  final EdmType leftType = getType(left);
  final EdmType rightType = getType(right);
  checkType(left,
      EdmPrimitiveTypeKind.Int16, EdmPrimitiveTypeKind.Int32, EdmPrimitiveTypeKind.Int64,
      EdmPrimitiveTypeKind.Byte, EdmPrimitiveTypeKind.SByte,
      EdmPrimitiveTypeKind.Decimal, EdmPrimitiveTypeKind.Single, EdmPrimitiveTypeKind.Double,
      EdmPrimitiveTypeKind.Boolean, EdmPrimitiveTypeKind.Guid, EdmPrimitiveTypeKind.String,
      EdmPrimitiveTypeKind.Date, EdmPrimitiveTypeKind.TimeOfDay,
      EdmPrimitiveTypeKind.DateTimeOffset, EdmPrimitiveTypeKind.Duration);
  checkType(right,
      EdmPrimitiveTypeKind.Int16, EdmPrimitiveTypeKind.Int32, EdmPrimitiveTypeKind.Int64,
      EdmPrimitiveTypeKind.Byte, EdmPrimitiveTypeKind.SByte,
      EdmPrimitiveTypeKind.Decimal, EdmPrimitiveTypeKind.Single, EdmPrimitiveTypeKind.Double,
      EdmPrimitiveTypeKind.Boolean, EdmPrimitiveTypeKind.Guid, EdmPrimitiveTypeKind.String,
      EdmPrimitiveTypeKind.Date, EdmPrimitiveTypeKind.TimeOfDay,
      EdmPrimitiveTypeKind.DateTimeOffset, EdmPrimitiveTypeKind.Duration);
  if (leftType == null || rightType == null) {
    return;
  }
  if (!(((EdmPrimitiveType) leftType).isCompatible((EdmPrimitiveType) rightType)
      || ((EdmPrimitiveType) rightType).isCompatible((EdmPrimitiveType) leftType))) {
    throw new UriParserSemanticException("Incompatible types.",
        UriParserSemanticException.MessageKeys.TYPES_NOT_COMPATIBLE,
        leftType.getFullQualifiedName().getFullQualifiedNameAsString(),
        rightType.getFullQualifiedName().getFullQualifiedNameAsString());
  }
}