Java Code Examples for org.apache.olingo.odata2.api.edm.EdmSimpleType#valueOfString()

The following examples show how to use org.apache.olingo.odata2.api.edm.EdmSimpleType#valueOfString() . 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: JPAFunctionContext.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private Object convertArgument(final EdmLiteral edmLiteral, final EdmFacets facets, final Class<?> targetType)
    throws EdmSimpleTypeException {
  Object value = null;
  if (edmLiteral != null) {
    EdmSimpleType edmType = edmLiteral.getType();
    value = edmType.valueOfString(edmLiteral.getLiteral(), EdmLiteralKind.DEFAULT, facets, targetType);
  }
  return value;
}
 
Example 2
Source File: XmlPropertyConsumer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private Object convert(final EntityPropertyInfo property, final String value, final Class<?> typeMapping,
    final EntityProviderReadProperties readProperties) throws EdmSimpleTypeException {
  final EdmSimpleType type = (EdmSimpleType) property.getType();
  return type.valueOfString(value, EdmLiteralKind.DEFAULT,
      readProperties == null || readProperties.isValidatingFacets() ? property.getFacets() : null,
      typeMapping == null ? type.getDefaultType() : typeMapping);
}
 
Example 3
Source File: EdmSimpleTypeTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private void expectErrorInValueOfString(final EdmSimpleType instance, final String value,
    final EdmLiteralKind literalKind, final EdmFacets facets, final MessageReference messageReference) {
  try {
    instance.valueOfString(value, literalKind, facets, instance.getDefaultType());
    fail("Expected exception not thrown");
  } catch (EdmSimpleTypeException e) {
    assertNotNull(e.getMessageReference());
    assertEquals(messageReference.getKey(), e.getMessageReference().getKey());
  }
}
 
Example 4
Source File: EdmSimpleTypeTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private void expectTypeErrorInValueOfString(final EdmSimpleType instance, final String value,
    final EdmLiteralKind literalKind) {
  try {
    instance.valueOfString(value, literalKind, null, Class.class);
    fail("Expected exception not thrown");
  } catch (EdmSimpleTypeException e) {
    assertNotNull(e.getMessageReference());
    assertEquals(EdmSimpleTypeException.VALUE_TYPE_NOT_SUPPORTED.getKey(), e.getMessageReference().getKey());
  }
}
 
Example 5
Source File: EdmSimpleTypeTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private void expectUnconvertibleErrorInValueOfString(final EdmSimpleType instance, final String value,
    final Class<?> type) {
  try {
    instance.valueOfString(value, EdmLiteralKind.DEFAULT, null, type);
    fail("Expected exception not thrown");
  } catch (EdmSimpleTypeException e) {
    assertNotNull(e.getMessageReference());
    assertEquals(EdmSimpleTypeException.LITERAL_UNCONVERTIBLE_TO_VALUE_TYPE.getKey(), e.getMessageReference()
        .getKey());
  }
}
 
Example 6
Source File: XmlPropertyDeserializer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private Object convert(final EntityPropertyInfo property, final String value, final Class<?> typeMapping,
    final DeserializerProperties readProperties) throws EdmSimpleTypeException {
  final EdmSimpleType type = (EdmSimpleType) property.getType();
  return type.valueOfString(value, EdmLiteralKind.DEFAULT,
      readProperties == null || readProperties.isValidatingFacets() ? property.getFacets() : null,
      typeMapping == null ? type.getDefaultType() : typeMapping);
}
 
Example 7
Source File: ODataParser.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public Object visitLiteral(LiteralExpression literal, EdmLiteral edmLiteral) {
    try {
        final EdmSimpleType type = edmLiteral.getType();

        final Object value = type.valueOfString(edmLiteral.getLiteral(),
            EdmLiteralKind.DEFAULT, null, type.getDefaultType());

        return new TypedValue(type.getDefaultType(), edmLiteral.getLiteral(), value);
    } catch (EdmSimpleTypeException ex) {
        throw new SearchParseException("Failed to convert literal to a typed form: " + literal, ex);
    }
}
 
Example 8
Source File: XmlEntryConsumer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private void readCustomElement(final XMLStreamReader reader, final String tagName, final EntityInfoAggregator eia,
    final EntityProviderReadProperties readProperties)
    throws EdmException, EntityProviderException, XMLStreamException {
  EntityPropertyInfo targetPathInfo = eia.getTargetPathInfo(tagName);
  NamespaceContext nsctx = reader.getNamespaceContext();

  boolean skipTag = true;
  if (!Edm.NAMESPACE_ATOM_2005.equals(reader.getName().getNamespaceURI())) {

    if (targetPathInfo != null) {
      final String customPrefix = targetPathInfo.getCustomMapping().getFcNsPrefix();
      final String customNamespaceURI = targetPathInfo.getCustomMapping().getFcNsUri();

      if (customPrefix != null && customNamespaceURI != null) {
        String xmlPrefix = nsctx.getPrefix(customNamespaceURI);
        String xmlNamespaceUri = reader.getNamespaceURI(customPrefix);

        if (customNamespaceURI.equals(xmlNamespaceUri) && customPrefix.equals(xmlPrefix)) {
          skipTag = false;
          reader.require(XMLStreamConstants.START_ELEMENT, customNamespaceURI, tagName);
          final String text = reader.getElementText();
          reader.require(XMLStreamConstants.END_ELEMENT, customNamespaceURI, tagName);

          final EntityPropertyInfo propertyInfo = getValidatedPropertyInfo(eia, tagName);
          final Class<?> typeMapping = typeMappings.getMappingClass(propertyInfo.getName());
          final EdmSimpleType type = (EdmSimpleType) propertyInfo.getType();
          final Object value = type.valueOfString(text, EdmLiteralKind.DEFAULT,
              readProperties == null || readProperties.isValidatingFacets() ? propertyInfo.getFacets() : null,
              typeMapping == null ? type.getDefaultType() : typeMapping);
          properties.put(tagName, value);
        }
      }
    } else {
      throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY.addContent(tagName));
    }
  }

  if (skipTag) {
    skipStartedTag(reader);
  }
}
 
Example 9
Source File: JsonPropertyConsumer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private Object readSimpleProperty(final JsonReader reader, final EntityPropertyInfo entityPropertyInfo,
    final Object typeMapping, final EntityProviderReadProperties readProperties)
    throws EdmException, EntityProviderException, IOException {
  final EdmSimpleType type = (EdmSimpleType) entityPropertyInfo.getType();
  Object value = null;
  final JsonToken tokenType = reader.peek();
  if (tokenType == JsonToken.NULL) {
    reader.nextNull();
  } else {
    switch (EdmSimpleTypeKind.valueOf(type.getName())) {
    case Boolean:
      if (tokenType == JsonToken.BOOLEAN) {
        value = reader.nextBoolean();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    case Byte:
    case SByte:
    case Int16:
    case Int32:
      if (tokenType == JsonToken.NUMBER) {
        value = reader.nextInt();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    case Single:
    case Double:
      if (tokenType == JsonToken.STRING) {
        value = reader.nextString();
      } else if (tokenType == JsonToken.NUMBER) {
        value = reader.nextDouble();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    default:
      if (tokenType == JsonToken.STRING) {
        value = reader.nextString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    }
  }

  final Class<?> typeMappingClass = typeMapping == null ? type.getDefaultType() : (Class<?>) typeMapping;
  final EdmFacets facets = readProperties == null || readProperties.isValidatingFacets() ?
      entityPropertyInfo.getFacets() : null;
  return type.valueOfString((String) value, EdmLiteralKind.JSON, facets, typeMappingClass);
}
 
Example 10
Source File: JsonPropertyDeserializer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private Object readSimpleProperty(final JsonReader reader, final EntityPropertyInfo entityPropertyInfo,
    final Object typeMapping, final DeserializerProperties readProperties)
    throws EdmException, EntityProviderException, IOException {
  final EdmSimpleType type = (EdmSimpleType) entityPropertyInfo.getType();
  Object value = null;
  final JsonToken tokenType = reader.peek();
  if (tokenType == JsonToken.NULL) {
    reader.nextNull();
  } else {
    switch (EdmSimpleTypeKind.valueOf(type.getName())) {
    case Boolean:
      if (tokenType == JsonToken.BOOLEAN) {
        value = reader.nextBoolean();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    case Byte:
    case SByte:
    case Int16:
    case Int32:
      if (tokenType == JsonToken.NUMBER) {
        value = reader.nextInt();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    case Single:
    case Double:
      if (tokenType == JsonToken.STRING) {
        value = reader.nextString();
      } else if (tokenType == JsonToken.NUMBER) {
        value = reader.nextDouble();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    default:
      if (tokenType == JsonToken.STRING) {
        value = reader.nextString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    }
  }

  final Class<?> typeMappingClass = typeMapping == null ? type.getDefaultType() : (Class<?>) typeMapping;
  final EdmFacets facets = readProperties == null || readProperties.isValidatingFacets() ?
      entityPropertyInfo.getFacets() : null;
  return type.valueOfString((String) value, EdmLiteralKind.JSON, facets, typeMappingClass);
}
 
Example 11
Source File: XmlEntryDeserializer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private void readCustomElement(final XMLStreamReader reader, final String tagName, //NOSONAR
    final EntityInfoAggregator eia,
    final DeserializerProperties readProperties)
    throws EdmException, EntityProviderException, XMLStreamException { //NOSONAR
  EntityPropertyInfo targetPathInfo = eia.getTargetPathInfo(tagName);
  NamespaceContext nsctx = reader.getNamespaceContext();

  boolean skipTag = true;
  if (!Edm.NAMESPACE_ATOM_2005.equals(reader.getName().getNamespaceURI())) {

    if (targetPathInfo != null) {
      final String customPrefix = targetPathInfo.getCustomMapping().getFcNsPrefix();
      final String customNamespaceURI = targetPathInfo.getCustomMapping().getFcNsUri();

      if (customPrefix != null && customNamespaceURI != null) {
        String xmlPrefix = nsctx.getPrefix(customNamespaceURI);
        String xmlNamespaceUri = reader.getNamespaceURI(customPrefix);

        if (customNamespaceURI.equals(xmlNamespaceUri) && customPrefix.equals(xmlPrefix)) { //NOSONAR
          skipTag = false;
          reader.require(XMLStreamConstants.START_ELEMENT, customNamespaceURI, tagName);
          final String text = reader.getElementText();
          reader.require(XMLStreamConstants.END_ELEMENT, customNamespaceURI, tagName);

          final EntityPropertyInfo propertyInfo = getValidatedPropertyInfo(eia, tagName);
          final Class<?> typeMapping = typeMappings.getMappingClass(propertyInfo.getName());
          final EdmSimpleType type = (EdmSimpleType) propertyInfo.getType();
          final Object value = type.valueOfString(text, EdmLiteralKind.DEFAULT,
              readProperties == null || readProperties.isValidatingFacets() ? propertyInfo.getFacets() : null,
              typeMapping == null ? type.getDefaultType() : typeMapping);
          properties.put(tagName, value);
        }
      }
    } else {
      throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY.addContent(tagName));
    }
  }

  if (skipTag) {
    skipStartedTag(reader);
  }
}
 
Example 12
Source File: CarODataSingleProcessor.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private int getKeyValue(final KeyPredicate key) throws ODataException {
  EdmProperty property = key.getProperty();
  EdmSimpleType type = (EdmSimpleType) property.getType();
  return type.valueOfString(key.getLiteral(), EdmLiteralKind.DEFAULT, property.getFacets(), Integer.class);
}
 
Example 13
Source File: MyODataSingleProcessor.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
private int getKeyValue(KeyPredicate key) throws ODataException {
    EdmProperty property = key.getProperty();
    EdmSimpleType type = (EdmSimpleType) property.getType();
    return type.valueOfString(key.getLiteral(), EdmLiteralKind.DEFAULT, property.getFacets(), Integer.class);
}