org.apache.olingo.odata2.api.uri.expression.MethodExpression Java Examples

The following examples show how to use org.apache.olingo.odata2.api.uri.expression.MethodExpression. 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: JsonVisitor.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public Object visitMethod(final MethodExpression methodExpression, final MethodOperator method,
    final List<Object> parameters) {
  try {
    StringWriter writer = new StringWriter();
    JsonStreamWriter jsonStreamWriter = new JsonStreamWriter(writer);
    jsonStreamWriter.beginObject().namedStringValueRaw("nodeType", methodExpression.getKind().toString()).separator()
        .namedStringValueRaw("operator", method.toUriLiteral()).separator().namedStringValueRaw("type",
            getType(methodExpression)).separator().name("parameters").beginArray();
    boolean first = true;
    for (Object parameter : parameters) {
      if (first) {
        first = false;
      } else {
        jsonStreamWriter.separator();
      }
      jsonStreamWriter.unquotedValue(parameter.toString());
    }
    jsonStreamWriter.endArray().endObject();
    writer.flush();
    return writer.toString();
  } catch (final IOException e) {
    return null;
  }
}
 
Example #2
Source File: VisitorTool.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public Object visitMethod(final MethodExpression methodExpression, final MethodOperator method,
    final List<Object> retParameters) {
  StringBuilder sb = new StringBuilder();
  sb.append("{");
  sb.append(method.toUriLiteral());

  sb.append("(");
  for (int i = 0; i < retParameters.size(); i++) {
    if (i != 0) {
      sb.append(",");
    }
    sb.append(retParameters.get(i));
  }

  sb.append(")}");

  return sb.toString();
}
 
Example #3
Source File: DebugInfoUri.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private void appendExpression(final CommonExpression expression, final Writer writer) throws IOException {
  final ExpressionKind kind = expression.getKind();
  writer.append("<span class=\"kind\">")
      .append(kind.toString())
      .append("</span> <span class=\"literal\">")
      .append(kind == ExpressionKind.MEMBER ? ((MemberExpression) expression).getProperty().getUriLiteral() :
          expression.getUriLiteral())
      .append("</span>, type <span class=\"type\">")
      .append(expression.getEdmType().toString())
      .append("</span>");
  if (kind == ExpressionKind.UNARY) {
    writer.append("<ul class=\"expr\"><li>");
    appendExpression(((UnaryExpression) expression).getOperand(), writer);
    writer.append("</li></ul>");
  } else if (kind == ExpressionKind.BINARY) {
    writer.append("<ol class=\"expr\"><li>");
    appendExpression(((BinaryExpression) expression).getLeftOperand(), writer);
    writer.append("</li><li>");
    appendExpression(((BinaryExpression) expression).getRightOperand(), writer);
    writer.append("</li></ol>");
  } else if (kind == ExpressionKind.METHOD) {
    final MethodExpression methodExpression = (MethodExpression) expression;
    if (methodExpression.getParameterCount() > 0) {
      writer.append("<ol class=\"expr\">");
      for (final CommonExpression parameter : methodExpression.getParameters()) {
        writer.append("<li>");
        appendExpression(parameter, writer);
        writer.append("</li>");
      }
      writer.append("</ol>");
    }
  } else if (kind == ExpressionKind.MEMBER) {
    writer.append("<ul class=\"expr\"><li>");
    appendExpression(((MemberExpression) expression).getPath(), writer);
    writer.append("</li></ul>");
  }
}
 
Example #4
Source File: FilterParserImpl.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
protected void validateMethodTypes(final MethodExpression methodExpression, final Token methodToken)
    throws ExpressionParserException, ExpressionParserInternalError {
  InfoMethod methOpt = availableMethods.get(methodExpression.getUriLiteral());

  List<EdmType> actualParameterTypes = new ArrayList<EdmType>();

  // If there are no parameter then don't perform a type check
  if (methodExpression.getParameters().isEmpty()) {
    return;
  }

  for (CommonExpression parameter : methodExpression.getParameters()) {
    // If there is not at parsing time its not possible to determine the type of eg myPropertyName.
    // Since this should not cause validation errors null type node arguments are leading to bypass
    // the validation
    if (parameter.getEdmType() == null && resourceEntityType == null) {
      return;
    }
    actualParameterTypes.add(parameter.getEdmType());
  }

  ParameterSet parameterSet = methOpt.validateParameterSet(actualParameterTypes);
  // If there is not returntype then the input parameter
  if (parameterSet == null) {
    // Tested with TestParserExceptions.testPMvalidateMethodTypes CASE 1
    throw FilterParserExceptionImpl.createMETHOD_WRONG_INPUT_TYPE((MethodExpressionImpl) methodExpression,
        methodToken, curExpression);
  }
  methodExpression.setEdmType(parameterSet.getReturnType());
}
 
Example #5
Source File: JPAQueryBuilderTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private UriInfo mockURIInfoForEntitySet(EdmMapping mapping, String methodName) throws EdmException {
  
  UriInfo uriInfo = EasyMock.createMock(UriInfo.class);
  List<NavigationSegment> navSegments = new ArrayList<NavigationSegment>();
  EasyMock.expect(uriInfo.getNavigationSegments()).andStubReturn(navSegments);
  EdmEntityType edmEntityType = EasyMock.createMock(EdmEntityType.class);
  EasyMock.expect(edmEntityType.getMapping()).andStubReturn(mapping);
  EdmEntitySet edmEntitySet = EasyMock.createMock(EdmEntitySet.class);
  EasyMock.expect(edmEntitySet.getEntityType()).andStubReturn(edmEntityType);
  EasyMock.expect(uriInfo.getTargetEntitySet()).andStubReturn(edmEntitySet);
  EdmProperty edmProperty = EasyMock.createMock(EdmProperty.class);
  EasyMock.expect(edmProperty.getMapping()).andStubReturn((EdmMapping)mockEdmMappingForProperty());
  
  EdmProperty edmProperty1 = EasyMock.createMock(EdmProperty.class);
  EasyMock.expect(edmProperty1.getMapping()).andStubReturn((EdmMapping)mockEdmMappingForProperty1());
  
  EdmNavigationProperty navEdmProperty = EasyMock.createMock(EdmNavigationProperty.class);
  EasyMock.expect(navEdmProperty.getMapping()).andStubReturn((EdmMapping)mockNavEdmMappingForProperty());
  
  OrderByExpression orderbyExpression = EasyMock.createMock(OrderByExpression.class);
  List<OrderExpression> orders = new ArrayList<OrderExpression>();
  EasyMock.expect(orderbyExpression.getOrders()).andStubReturn(orders);
  EasyMock.expect(uriInfo.getOrderBy()).andStubReturn(orderbyExpression);
  FilterExpression filterExpression = EasyMock.createMock(FilterExpression.class);
  MethodExpression commonExpression = EasyMock.createMock(MethodExpression.class);
  EasyMock.expect(commonExpression.getKind()).andStubReturn(ExpressionKind.METHOD);
  PropertyExpression propExp = EasyMock.createMock(PropertyExpression.class);
  LiteralExpression literalExp = EasyMock.createMock(LiteralExpression.class);
  MemberExpression memberExp = EasyMock.createMock(MemberExpression.class);
  List<CommonExpression> parameterList = new ArrayList<CommonExpression>();
  
  PropertyExpression navPropExp = EasyMock.createMock(PropertyExpression.class);
  if ("substringof".equals(methodName)) {
    EasyMock.expect(commonExpression.getMethod()).andStubReturn(MethodOperator.SUBSTRINGOF);
    EasyMock.expect(commonExpression.getParameterCount()).andStubReturn(2);
    EasyMock.expect(literalExp.getUriLiteral()).andStubReturn("'a.b.c'");
    EasyMock.expect(literalExp.getKind()).andStubReturn(ExpressionKind.LITERAL);
    EasyMock.expect(literalExp.getEdmType()).andStubReturn(EdmSimpleTypeKind.String.getEdmSimpleTypeInstance());
    parameterList.add(literalExp);
    EasyMock.expect(propExp.getEdmProperty()).andStubReturn(edmProperty);
    EasyMock.expect(propExp.getKind()).andStubReturn(ExpressionKind.PROPERTY);
    parameterList.add(propExp);
  } else if ("startsWith".equals(methodName)) {
    EasyMock.expect(commonExpression.getMethod()).andStubReturn(MethodOperator.STARTSWITH);
    EasyMock.expect(commonExpression.getParameterCount()).andStubReturn(2);
    EasyMock.expect(propExp.getEdmProperty()).andStubReturn(edmProperty);
    EasyMock.expect(propExp.getKind()).andStubReturn(ExpressionKind.PROPERTY);
    parameterList.add(propExp);
    EasyMock.expect(literalExp.getUriLiteral()).andStubReturn("'a.b.c'");
    EasyMock.expect(literalExp.getKind()).andStubReturn(ExpressionKind.LITERAL);
    EasyMock.expect(literalExp.getEdmType()).andStubReturn(EdmSimpleTypeKind.String.getEdmSimpleTypeInstance());
    parameterList.add(literalExp);
  } else if ("endsWith".equals(methodName)) {
    EasyMock.expect(commonExpression.getMethod()).andStubReturn(MethodOperator.ENDSWITH);
    EasyMock.expect(commonExpression.getParameterCount()).andStubReturn(2);
    EasyMock.expect(propExp.getEdmProperty()).andStubReturn(edmProperty);
    EasyMock.expect(propExp.getKind()).andStubReturn(ExpressionKind.PROPERTY);
    parameterList.add(propExp);
    EasyMock.expect(literalExp.getUriLiteral()).andStubReturn("'a.b.c'");
    EasyMock.expect(literalExp.getKind()).andStubReturn(ExpressionKind.LITERAL);
    EasyMock.expect(literalExp.getEdmType()).andStubReturn(EdmSimpleTypeKind.String.getEdmSimpleTypeInstance());
    parameterList.add(literalExp);
  } else if ("substringof_1".equals(methodName)) {
    EasyMock.expect(commonExpression.getMethod()).andStubReturn(MethodOperator.SUBSTRINGOF);
    EasyMock.expect(commonExpression.getParameterCount()).andStubReturn(2);
    EasyMock.expect(literalExp.getUriLiteral()).andStubReturn("'a.b.c'");
    EasyMock.expect(literalExp.getKind()).andStubReturn(ExpressionKind.LITERAL);
    EasyMock.expect(literalExp.getEdmType()).andStubReturn(EdmSimpleTypeKind.String.getEdmSimpleTypeInstance());
    parameterList.add(literalExp);
    EasyMock.expect(memberExp.getKind()).andStubReturn(ExpressionKind.MEMBER);
    EasyMock.expect(memberExp.getProperty()).andStubReturn(propExp);
    
    EasyMock.expect(memberExp.getPath()).andStubReturn(navPropExp);
    EasyMock.expect(navPropExp.getKind()).andStubReturn(ExpressionKind.PROPERTY);
    EasyMock.expect(navPropExp.getEdmProperty()).andStubReturn(navEdmProperty);
    EasyMock.expect(propExp.getKind()).andStubReturn(ExpressionKind.PROPERTY);
    EasyMock.expect(propExp.getEdmProperty()).andStubReturn(edmProperty1);
    
    parameterList.add(propExp);
  }
  
  EasyMock.expect(commonExpression.getParameters()).andStubReturn(parameterList);
  
  EasyMock.expect(filterExpression.getExpression()).andStubReturn(commonExpression);
  EasyMock.expect(filterExpression.getKind()).andStubReturn(ExpressionKind.FILTER);
  EasyMock.expect(filterExpression.getExpressionString()).andStubReturn("substringof('a.b.c',Id)");
  EasyMock.expect(uriInfo.getFilter()).andStubReturn(filterExpression);
  EasyMock.replay(edmEntityType, edmEntitySet, orderbyExpression, filterExpression, 
      commonExpression, literalExp, propExp, uriInfo, edmProperty, memberExp,
      edmProperty1, navEdmProperty);
  return uriInfo;

}
 
Example #6
Source File: JPAQueryBuilderTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private UriInfo mockURIInfoForEntitySetWithBinaryFilterExpression
(EdmMapping mapping, String methodName) throws EdmException {
  
  UriInfo uriInfo = EasyMock.createMock(UriInfo.class);
  List<NavigationSegment> navSegments = new ArrayList<NavigationSegment>();
  EasyMock.expect(uriInfo.getNavigationSegments()).andStubReturn(navSegments);
  EdmEntityType edmEntityType = EasyMock.createMock(EdmEntityType.class);
  EasyMock.expect(edmEntityType.getMapping()).andStubReturn(mapping);
  EdmEntitySet edmEntitySet = EasyMock.createMock(EdmEntitySet.class);
  EasyMock.expect(edmEntitySet.getEntityType()).andStubReturn(edmEntityType);
  EasyMock.expect(uriInfo.getTargetEntitySet()).andStubReturn(edmEntitySet);
  EdmProperty edmProperty = EasyMock.createMock(EdmProperty.class);
  EasyMock.expect(edmProperty.getMapping()).andStubReturn((EdmMapping)mockEdmMappingForProperty());
  OrderByExpression orderbyExpression = EasyMock.createMock(OrderByExpression.class);
  List<OrderExpression> orders = new ArrayList<OrderExpression>();
  EasyMock.expect(orderbyExpression.getOrders()).andStubReturn(orders);
  EasyMock.expect(uriInfo.getOrderBy()).andStubReturn(orderbyExpression);
  FilterExpression filterExpression = EasyMock.createMock(FilterExpression.class);
  BinaryExpression commonExpression = EasyMock.createMock(BinaryExpression.class);
  EasyMock.expect(commonExpression.getOperator()).andStubReturn(BinaryOperator.EQ);
  EasyMock.expect(commonExpression.getKind()).andStubReturn(ExpressionKind.BINARY);
  MethodExpression methodExp = EasyMock.createMock(MethodExpression.class);
  EasyMock.expect(commonExpression.getLeftOperand()).andStubReturn(methodExp);
  EdmSimpleType type = EasyMock.createMock(EdmSimpleType.class);;
  EasyMock.expect(methodExp.getEdmType()).andStubReturn(type );
  
  LiteralExpression literalValueExp = EasyMock.createMock(LiteralExpression.class);
  EasyMock.expect(commonExpression.getRightOperand()).andStubReturn(literalValueExp);
  EasyMock.expect(literalValueExp.getUriLiteral()).andStubReturn("'a%.b*.c'");
  EasyMock.expect(literalValueExp.getKind()).andStubReturn(ExpressionKind.LITERAL);
  EasyMock.expect(literalValueExp.getEdmType()).andStubReturn(EdmSimpleTypeKind.String.getEdmSimpleTypeInstance());
  
  PropertyExpression propExp = EasyMock.createMock(PropertyExpression.class);
  LiteralExpression literalExp1 = EasyMock.createMock(LiteralExpression.class);
  LiteralExpression literalExp2 = EasyMock.createMock(LiteralExpression.class);
  List<CommonExpression> parameterList = new ArrayList<CommonExpression>();
  if ("substring".equals(methodName)) {
    EasyMock.expect(methodExp.getMethod()).andStubReturn(MethodOperator.SUBSTRING);
    EasyMock.expect(methodExp.getKind()).andStubReturn(ExpressionKind.METHOD);
    EasyMock.expect(methodExp.getParameterCount()).andStubReturn(3);
    EasyMock.expect(propExp.getEdmProperty()).andStubReturn(edmProperty);
    EasyMock.expect(propExp.getKind()).andStubReturn(ExpressionKind.PROPERTY);
    parameterList.add(propExp);
    EasyMock.expect(literalExp1.getUriLiteral()).andStubReturn("1");
    EasyMock.expect(literalExp1.getKind()).andStubReturn(ExpressionKind.LITERAL);
    EasyMock.expect(literalExp1.getEdmType()).andStubReturn(EdmSimpleTypeKind.Int16.getEdmSimpleTypeInstance());
    parameterList.add(literalExp1);
    EasyMock.expect(literalExp2.getUriLiteral()).andStubReturn("2");
    EasyMock.expect(literalExp2.getKind()).andStubReturn(ExpressionKind.LITERAL);
    EasyMock.expect(literalExp2.getEdmType()).andStubReturn(EdmSimpleTypeKind.Int16.getEdmSimpleTypeInstance());
    parameterList.add(literalExp2);
    EasyMock.expect(methodExp.getParameters()).andStubReturn(parameterList);
  } else if ("toLower".equals(methodName)) {
    EasyMock.expect(methodExp.getMethod()).andStubReturn(MethodOperator.TOLOWER);
    EasyMock.expect(methodExp.getKind()).andStubReturn(ExpressionKind.METHOD);
    EasyMock.expect(methodExp.getParameterCount()).andStubReturn(1);
    EasyMock.expect(propExp.getEdmProperty()).andStubReturn(edmProperty);
    EasyMock.expect(propExp.getKind()).andStubReturn(ExpressionKind.PROPERTY);
    parameterList.add(propExp);
    EasyMock.expect(methodExp.getParameters()).andStubReturn(parameterList);
  } else if ("toUpper".equals(methodName)) {
    EasyMock.expect(methodExp.getMethod()).andStubReturn(MethodOperator.TOUPPER);
    EasyMock.expect(methodExp.getKind()).andStubReturn(ExpressionKind.METHOD);
    EasyMock.expect(methodExp.getParameterCount()).andStubReturn(1);
    EasyMock.expect(propExp.getEdmProperty()).andStubReturn(edmProperty);
    EasyMock.expect(propExp.getKind()).andStubReturn(ExpressionKind.PROPERTY);
    parameterList.add(propExp);
    EasyMock.expect(methodExp.getParameters()).andStubReturn(parameterList);
  }
  EasyMock.expect(filterExpression.getExpression()).andStubReturn(commonExpression);
  EasyMock.expect(filterExpression.getKind()).andStubReturn(ExpressionKind.FILTER);
  EasyMock.expect(filterExpression.getExpressionString()).andStubReturn("substring(CompanyName,1,2) eq 'a.b.c'");
  EasyMock.expect(uriInfo.getFilter()).andStubReturn(filterExpression);
  EasyMock.replay(edmEntityType, edmEntitySet, orderbyExpression, filterExpression, 
      commonExpression, literalExp1, literalExp2, propExp, uriInfo, edmProperty, 
      methodExp, literalValueExp);
  return uriInfo;

}
 
Example #7
Source File: FilterParserImpl.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
/**
 * Read the parameters of a method expression
 * @param methodInfo
 * Signature information about the method whose parameters should be read
 * @param methodExpression
 * Method expression to which the read parameters are added
 * @return
 * The method expression input parameter
 * @throws ExpressionParserException
 * @throws ExpressionParserInternalError
 * @throws TokenizerExpectError
 * The next token did not match the expected token
 */
protected MethodExpression readParameters(final InfoMethod methodInfo, final MethodExpressionImpl methodExpression,
    final Token methodToken) throws ExpressionParserException, ExpressionParserInternalError {
  CommonExpression expression;
  boolean expectAnotherExpression = false;
  boolean readComma = true;

  // The existing of a '(' is verified BEFORE this method is called --> so it's a internal error
  Token openParenthesis = tokenList.expectToken(TokenKind.OPENPAREN, true); // throws FilterParserInternalError

  Token token = tokenList.lookToken();
  if (token == null) {
    // Tested with TestParserExceptions.TestPMreadParameters CASE 1 e.g. "$filter=concat("
    throw FilterParserExceptionImpl.createEXPRESSION_EXPECTED_AFTER_POS(openParenthesis, curExpression);
  }

  while (token.getKind() != TokenKind.CLOSEPAREN) {
    if (readComma == false) {
      // Tested with TestParserExceptions.TestPMreadParameters CASE 12 e.g. "$filter=concat('a' 'b')"
      throw FilterParserExceptionImpl.createCOMMA_OR_CLOSING_PARENTHESIS_EXPECTED_AFTER_POS(tokenList
          .lookPrevToken(), curExpression);
    }
    expression = readElement(null);
    if (expression != null) {
      expression = readElements(expression, 0);
    }

    if ((expression == null) && (expectAnotherExpression == true)) {
      // Tested with TestParserExceptions.TestPMreadParameters CASE 4 e.g. "$filter=concat(,"
      throw FilterParserExceptionImpl.createEXPRESSION_EXPECTED_AFTER_POS(token, curExpression);
    } else if (expression != null) {// parameter list may be empty
      methodExpression.appendParameter(expression);
    }

    token = tokenList.lookToken();
    if (token == null) {
      // Tested with TestParserExceptions.TestPMreadParameters CASE 2 e.g. "$filter=concat(123"
      throw FilterParserExceptionImpl.createCOMMA_OR_CLOSING_PARENTHESIS_EXPECTED_AFTER_POS(tokenList
          .lookPrevToken(), curExpression);
    }

    if (token.getKind() == TokenKind.COMMA) {
      expectAnotherExpression = true;
      if (expression == null) {
        // Tested with TestParserExceptions.TestPMreadParameters CASE 3 e.g. "$filter=concat(,"
        throw FilterParserExceptionImpl.createEXPRESSION_EXPECTED_AT_POS(token, curExpression);
      }

      tokenList.expectToken(",", true);
      readComma = true;
    } else {
      readComma = false;
    }
  }

  // because the while loop above only exits if a ')' has been found it is an
  // internal error if there is not ')'
  tokenList.expectToken(TokenKind.CLOSEPAREN, true);

  // ---check parameter count
  int count = methodExpression.getParameters().size();
  if ((methodInfo.getMinParameter() > -1) && (count < methodInfo.getMinParameter())) {
    // Tested with TestParserExceptions.TestPMreadParameters CASE 12
    throw FilterParserExceptionImpl.createMETHOD_WRONG_ARG_COUNT(methodExpression, methodToken, curExpression);
  }

  if ((methodInfo.getMaxParameter() > -1) && (count > methodInfo.getMaxParameter())) {
    // Tested with TestParserExceptions.TestPMreadParameters CASE 15
    throw FilterParserExceptionImpl.createMETHOD_WRONG_ARG_COUNT(methodExpression, methodToken, curExpression);
  }

  return methodExpression;
}
 
Example #8
Source File: ODataParser.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
public Object visitMethod(MethodExpression methodExpression, MethodOperator method, List<Object> parameters) {
    throw new SearchParseException("Unsupported operation visitMethod: " + methodExpression
        + "," + method + "," + parameters);
}