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

The following examples show how to use org.apache.olingo.server.api.uri.queryoption.expression.Literal. 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: DataProvider.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private Map<String, Parameter> getFunctionParameters(final EdmFunction function,
    final List<UriParameter> parameters, final UriInfoResource uriInfo) throws DataProviderException {
  Map<String, Parameter> values = new HashMap<String, Parameter>();
  for (final UriParameter parameter : parameters) {
    if (parameter.getExpression() != null && !(parameter.getExpression() instanceof Literal)) {
      throw new DataProviderException("Expression in function-parameter value is not supported yet!",
          HttpStatusCode.NOT_IMPLEMENTED);
    }
    final EdmParameter edmParameter = function.getParameter(parameter.getName());
    final String text = parameter.getAlias() == null ?
        parameter.getText() :
        uriInfo.getValueForAlias(parameter.getAlias());
    if (text != null) {
      try {
        values.put(parameter.getName(),
            odata.createFixedFormatDeserializer().parameter(text, edmParameter));
      } catch (final DeserializerException e) {
        throw new DataProviderException("Invalid function parameter.", HttpStatusCode.BAD_REQUEST, e);
      }
    }
  }
  return values;
}
 
Example #2
Source File: FilterValidator.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public FilterValidator isType(final FullQualifiedName fullName) {
  EdmType actualType = null;

  if (curExpression instanceof Member) {
    actualType = ((Member) curExpression).getType();
  } else if (curExpression instanceof TypeLiteral) {
    actualType = ((TypeLiteral) curExpression).getType();
  } else if (curExpression instanceof Literal) {
    actualType = ((Literal) curExpression).getType();
  } else if (curExpression instanceof Enumeration) {
    actualType = ((Enumeration) curExpression).getType();
  } else if (curExpression instanceof Unary) {
    actualType = ((UnaryImpl) curExpression).getType();
  } else if (curExpression instanceof Binary) {
    actualType = ((BinaryImpl) curExpression).getType();
  } else if (curExpression instanceof Method) {
    actualType = ((MethodImpl) curExpression).getType();
  }

  assertNotNull("Current expression not typed", actualType);
  assertEquals(fullName, actualType.getFullQualifiedName());
  return this;
}
 
Example #3
Source File: FilterExpressionVisitor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
public Object visitLiteral(Literal literal) throws ExpressionVisitException, ODataApplicationException {
  // To keep this tutorial simple, our filter expression visitor supports only Edm.Int32 and Edm.String
  // In real world scenarios it can be difficult to guess the type of an literal.
  // We can be sure, that the literal is a valid OData literal because the URI Parser checks 
  // the lexicographical structure
  
  // String literals start and end with an single quotation mark
  String literalAsString = literal.getText();
  if(literal.getType() instanceof EdmString) {
    String stringLiteral = "";
    if(literal.getText().length() > 2) {
      stringLiteral = literalAsString.substring(1, literalAsString.length() - 1);
    }
    
    return stringLiteral;
  } else {
    // Try to convert the literal into an Java Integer
    try {
      return Integer.parseInt(literalAsString);
    } catch(NumberFormatException e) {
      throw new ODataApplicationException("Only Edm.Int32 and Edm.String literals are implemented", 
          HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
    }
  }
}
 
Example #4
Source File: FilterExpressionVisitor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public Object visitLiteral(Literal literal) throws ExpressionVisitException, ODataApplicationException {
  // To keep this tutorial simple, our filter expression visitor supports only Edm.Int32 and Edm.String
  // In real world scenarios it can be difficult to guess the type of an literal.
  // We can be sure, that the literal is a valid OData literal because the URI Parser checks 
  // the lexicographical structure
  
  // String literals start and end with an single quotation mark
  String literalAsString = literal.getText();
  if(literal.getType() instanceof EdmString) {
    String stringLiteral = "";
    if(literal.getText().length() > 2) {
      stringLiteral = literalAsString.substring(1, literalAsString.length() - 1);
    }
    
    return stringLiteral;
  } else {
    // Try to convert the literal into an Java Integer
    try {
      return Integer.parseInt(literalAsString);
    } catch(NumberFormatException e) {
      throw new ODataApplicationException("Only Edm.Int32 and Edm.String literals are implemented", 
          HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
    }
  }
}
 
Example #5
Source File: FilterExpressionVisitor.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Override
public Object visitLiteral(Literal literal) throws ExpressionVisitException, ODataApplicationException {
    // To keep this tutorial simple, our filter expression visitor supports
    // only Edm.Int32 and Edm.String
    // In real world scenarios it can be difficult to guess the type of an
    // literal.
    // We can be sure, that the literal is a valid OData literal because the
    // URI Parser checks
    // the lexicographical structure

    // String literals start and end with an single quotation mark
    String literalAsString = literal.getText();
    if (literal.getType() instanceof EdmString) {
        String stringLiteral = "";
        if (literal.getText().length() > 2) {
            stringLiteral = literalAsString.substring(1, literalAsString.length() - 1);
        }

        return stringLiteral;
    }

    // Try to convert the literal into an Java Integer
    try {
        return Integer.parseInt(literalAsString);
    } catch (NumberFormatException e) {
        throw new ODataApplicationException("Only Edm.Int32 and Edm.String literals are implemented",
            HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH, e);
    }
}
 
Example #6
Source File: ExpressionJsonVisitor.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Override
public JsonNode visitLiteral(final Literal literal) throws ExpressionVisitException, ODataApplicationException {
  return nodeFactory.objectNode()
      .put(NODE_TYPE_NAME, LITERAL_NAME)
      .put(TYPE_NAME, getTypeString(literal.getType()))
      .put(VALUE_NAME, literal.getText());
}
 
Example #7
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 #8
Source File: ExpressionVisitorImpl.java    From micro-integrator with Apache License 2.0 4 votes vote down vote up
@Override
public VisitorOperand visitLiteral(Literal literal) throws ExpressionVisitException, ODataApplicationException {
    return new UntypedOperand(literal.getText());
}
 
Example #9
Source File: ODataFesParser.java    From arctic-sea with Apache License 2.0 4 votes vote down vote up
@Override
public StringValueExpr visitLiteral(Literal literal) {
    return new StringValueExpr(stripQuotes(literal.getText()));
}
 
Example #10
Source File: DataProvider.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public Entity read(final EdmEntityType edmEntityType, final EntityCollection entitySet,
    final List<UriParameter> keys) throws DataProviderException {
  try {
    for (final Entity entity : entitySet.getEntities()) {
      boolean found = true;
      for (final UriParameter key : keys) {
        EdmKeyPropertyRef refType = edmEntityType.getKeyPropertyRef(key.getName());
        Object value =  findPropertyRefValue(entity, refType);
        
        final EdmProperty property = refType.getProperty();
        final EdmPrimitiveType type = (EdmPrimitiveType) property.getType();
        
        if (key.getExpression() != null && !(key.getExpression() instanceof Literal)) {
          throw new DataProviderException("Expression in key value is not supported yet!",
              HttpStatusCode.NOT_IMPLEMENTED);
        }
        Object keyValue = null;
        final String text = key.getAlias() == null ? key.getText() : ((Literal) key.getExpression()).getText();
        if (Calendar.class.isAssignableFrom(value.getClass())) {
          keyValue = type.valueOfString(type.fromUriLiteral(text),
              property.isNullable(), property.getMaxLength(), property.getPrecision(), property.getScale(),
              property.isUnicode(), Calendar.class);
        } else {
          keyValue = type.valueOfString(type.fromUriLiteral(text),
              property.isNullable(), property.getMaxLength(), property.getPrecision(), property.getScale(),
              property.isUnicode(), value.getClass());
        }
        if (!value.equals(keyValue)) {
          found = false;
          break;
        }
      }
      if (found) {
        return entity;
      }
    }
    return null;
  } catch (final EdmPrimitiveTypeException e) {
    throw new DataProviderException("Wrong key!", HttpStatusCode.BAD_REQUEST, e);
  }
}
 
Example #11
Source File: DataProvider.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public Entity readDataFromEntity(final EdmEntityType edmEntityType,
    final List<UriParameter> keys) throws DataProviderException {
  EntityCollection coll = data.get(edmEntityType.getName());
  List<Entity> entities = coll.getEntities();
  try {
    for (final Entity entity : entities) {
      boolean found = true;
      for (final UriParameter key : keys) {
        EdmKeyPropertyRef refType = edmEntityType.getKeyPropertyRef(key.getName());
        Object value =  findPropertyRefValue(entity, refType);
        
        final EdmProperty property = refType.getProperty();
        final EdmPrimitiveType type = (EdmPrimitiveType) property.getType();
        
        if (key.getExpression() != null && !(key.getExpression() instanceof Literal)) {
          throw new DataProviderException("Expression in key value is not supported yet!",
              HttpStatusCode.NOT_IMPLEMENTED);
        }
        final String text = key.getAlias() == null ? key.getText() : ((Literal) key.getExpression()).getText();
        Object keyValue = null;
        if (Calendar.class.isAssignableFrom(value.getClass())) {
          keyValue = type.valueOfString(type.fromUriLiteral(text),
              property.isNullable(), property.getMaxLength(), property.getPrecision(), property.getScale(),
              property.isUnicode(), Calendar.class);
        } else {
          keyValue = type.valueOfString(type.fromUriLiteral(text),
              property.isNullable(), property.getMaxLength(), property.getPrecision(), property.getScale(),
              property.isUnicode(), value.getClass());
        }
        if (!value.equals(keyValue)) {
          found = false;
          break;
        }
      }
      if (found) {
        return entity;
      }
    }
    return null;
  } catch (final EdmPrimitiveTypeException e) {
    throw new DataProviderException("Wrong key!", HttpStatusCode.BAD_REQUEST, e);
  }
}
 
Example #12
Source File: ExpressionVisitorImpl.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Override
public VisitorOperand visitLiteral(final Literal literal) throws ExpressionVisitException, ODataApplicationException {
  return new UntypedOperand(literal.getText());
}
 
Example #13
Source File: FilterTreeToText.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Override
public String visitLiteral(final Literal literal) throws ExpressionVisitException {
  return "<" + literal.getText() + ">";
}
 
Example #14
Source File: FilterValidator.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public FilterValidator isLiteral(final String literalText) {
  assertTrue("Current expression is not a literal", curExpression instanceof Literal);
  String actualLiteralText = ((Literal) curExpression).getText();
  assertEquals(literalText, actualLiteralText);
  return this;
}
 
Example #15
Source File: FilterValidator.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public FilterValidator isLiteralType(final EdmType edmType) {
  assertTrue("Current expression is not a literal", curExpression instanceof Literal);
  final EdmType type = ((Literal) curExpression).getType();
  assertEquals(edmType, type);
  return this;
}
 
Example #16
Source File: ParserHelper.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
protected static List<UriParameter> parseFunctionParameters(UriTokenizer tokenizer,
    final Edm edm, final EdmType referringType, final boolean withComplex,
    final Map<String, AliasQueryOption> aliases)
    throws UriParserException, UriValidationException {
  List<UriParameter> parameters = new ArrayList<>();
  Set<String> parameterNames = new HashSet<>();
  ParserHelper.requireNext(tokenizer, TokenKind.OPEN);
  if (tokenizer.next(TokenKind.CLOSE)) {
    return parameters;
  }
  do {
    ParserHelper.requireNext(tokenizer, TokenKind.ODataIdentifier);
    final String name = tokenizer.getText();
    if (parameterNames.contains(name)) {
      throw new UriParserSemanticException("Duplicated function parameter " + name,
          UriParserSemanticException.MessageKeys.INVALID_KEY_VALUE, name);
    }
    parameterNames.add(name);
    ParserHelper.requireNext(tokenizer, TokenKind.EQ);
    if (tokenizer.next(TokenKind.COMMA) || tokenizer.next(TokenKind.CLOSE) || tokenizer.next(TokenKind.EOF)) {
      throw new UriParserSyntaxException("Parameter value expected.", UriParserSyntaxException.MessageKeys.SYNTAX);
    }
    UriParameterImpl parameter = new UriParameterImpl().setName(name);
    if (tokenizer.next(TokenKind.ParameterAliasName)) {
      final String aliasName = tokenizer.getText();
      parameter.setAlias(aliasName)
          .setExpression(aliases.containsKey(aliasName) ? aliases.get(aliasName).getValue() : null);
    } else if (tokenizer.next(TokenKind.jsonArrayOrObject)) {
      if (withComplex) {
        parameter.setText(tokenizer.getText());
      } else {
        throw new UriParserSemanticException("A JSON array or object is not allowed as parameter value.",
            UriParserSemanticException.MessageKeys.COMPLEX_PARAMETER_IN_RESOURCE_PATH, tokenizer.getText());
      }
    } else if (withComplex) {
      final Expression expression = new ExpressionParser(edm, odata).parse(tokenizer, referringType, null, aliases);
      parameter.setText(expression instanceof Literal ?
          "null".equals(((Literal) expression).getText()) ? null : ((Literal) expression).getText() :
          null)
          .setExpression(expression instanceof Literal ? null : expression);
    } else if (nextPrimitiveValue(tokenizer) == null) {
      throw new UriParserSemanticException("Wrong parameter value.",
          UriParserSemanticException.MessageKeys.INVALID_KEY_VALUE, "");
    } else {
      final String literalValue = tokenizer.getText();
      parameter.setText("null".equals(literalValue) ? null : literalValue);
    }
    parameters.add(parameter);
  } while (tokenizer.next(TokenKind.COMMA));
  ParserHelper.requireNext(tokenizer, TokenKind.CLOSE);
  return parameters;
}