Java Code Examples for org.apache.olingo.odata2.api.edm.EdmLiteralKind#URI

The following examples show how to use org.apache.olingo.odata2.api.edm.EdmLiteralKind#URI . 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: EdmString.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
protected <T> T internalValueOfString(final String value, final EdmLiteralKind literalKind, final EdmFacets facets,
    final Class<T> returnType) throws EdmSimpleTypeException {
  String result;
  if (literalKind == EdmLiteralKind.URI) {
    if (value.length() >= 2 && value.startsWith("'") && value.endsWith("'")) {
      result = (value.substring(1, value.length() - 1)).replace("''", "'");
    } else {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value));
    }
  } else {
    result = value;
  }

  if (facets != null
      && (facets.isUnicode() != null && !facets.isUnicode() && !PATTERN_ASCII.matcher(result).matches()
      || facets.getMaxLength() != null && facets.getMaxLength()!=0 && facets.getMaxLength() < result.length())) {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_FACETS_NOT_MATCHED.addContent(value, facets));
  }

  if (returnType.isAssignableFrom(String.class)) {
    return returnType.cast(result);
  } else {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_TYPE_NOT_SUPPORTED.addContent(returnType));
  }
}
 
Example 2
Source File: EdmGuid.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
protected <T> T internalValueOfString(final String value, final EdmLiteralKind literalKind, final EdmFacets facets,
    final Class<T> returnType) throws EdmSimpleTypeException {
  UUID result;
  String cleanValue = null;
  if (literalKind == EdmLiteralKind.URI && value.toLowerCase().startsWith("guid'") && value.endsWith("'")) {
    cleanValue = value.substring(5, value.length() - 1);
  } else {
    cleanValue = value;
  }
  if (validateLiteral(cleanValue)) {
    result = UUID.fromString(cleanValue);
  } else {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value));
  }

  if (returnType.isAssignableFrom(UUID.class)) {
    return returnType.cast(result);
  } else {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_TYPE_NOT_SUPPORTED.addContent(returnType));
  }
}
 
Example 3
Source File: AbstractSimpleType.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public final String valueToString(final Object value, final EdmLiteralKind literalKind, final EdmFacets facets)
    throws EdmSimpleTypeException {
  if (value == null) {
    if (facets == null || facets.isNullable() == null || facets.isNullable()) {
      return null;
    } else {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_NULL_NOT_ALLOWED);
    }
  }

  if (literalKind == null) {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_KIND_MISSING);
  }

  final String result = internalValueToString(value, literalKind, facets);
  return literalKind == EdmLiteralKind.URI ? toUriLiteral(result) : result;
}
 
Example 4
Source File: EdmBinary.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private static boolean
    validateMaxLength(final String value, final EdmLiteralKind literalKind, final EdmFacets facets) {
  return facets == null || facets.getMaxLength() == null ? true :
      literalKind == EdmLiteralKind.URI ?
          // In URI representation, each byte is represented as two hexadecimal digits;
          // additionally, we have to account for the prefix and the surrounding "'"s.
          facets.getMaxLength() >= (value.length() - (value.startsWith("X") ? 3 : 8)) / 2
          :
          // In default representation, every three bytes are represented as four base-64 characters.
          // Additionally, there could be up to two padding "=" characters if the number of bytes is
          // not a multiple of three, and there could be line feeds, possibly with carriage returns.
          facets.getMaxLength() * 4L >= (value.length() - crlfLength(value)) * 3L
              - (value.contains("==") ? 2 : value.contains("=") ? 1 : 0) * 4L;
}
 
Example 5
Source File: EdmBinary.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
protected <T> T internalValueOfString(final String value, final EdmLiteralKind literalKind, final EdmFacets facets,
    final Class<T> returnType) throws EdmSimpleTypeException {
  if (!validateLiteral(value, literalKind)) {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value));
  }
  if (!validateMaxLength(value, literalKind, facets)) {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_FACETS_NOT_MATCHED.addContent(value, facets));
  }

  byte[] result;
  if (literalKind == EdmLiteralKind.URI) {
    try {
      result = Hex.decodeHex(value.substring(value.startsWith("X") ? 2 : 7, value.length() - 1).toCharArray());
    } catch (final DecoderException e) {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value), e);
    }
  } else {
    result = Base64.decodeBase64(value);
  }

  if (returnType.isAssignableFrom(byte[].class)) {
    return returnType.cast(result);
  } else if (returnType.isAssignableFrom(Byte[].class)) {
    Byte[] byteArray = new Byte[result.length];
    for (int i = 0; i < result.length; i++) {
      byteArray[i] = result[i];
    }
    return returnType.cast(byteArray);
  } else {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_TYPE_NOT_SUPPORTED.addContent(returnType));
  }
}
 
Example 6
Source File: EdmGuid.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private boolean validateLiteralInternal(String value, EdmLiteralKind literalKind) {
  String cleanValue = null;
  if (literalKind == EdmLiteralKind.URI && value.toLowerCase().startsWith("guid'") && value.endsWith("'")) {
    cleanValue = value.substring(5, value.length() - 1);
  } else {
    cleanValue = value;
  }
  return validateLiteral(cleanValue);
}
 
Example 7
Source File: EdmInt64.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> T internalValueOfString(final String value, final EdmLiteralKind literalKind, final EdmFacets facets,
    final Class<T> returnType) throws EdmSimpleTypeException {
  Long valueLong;
  try {
    if (literalKind == EdmLiteralKind.URI) {
      if (value.endsWith("L") || value.endsWith("l")) {
        valueLong = Long.parseLong(value.substring(0, value.length() - 1));
      } else {
        throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value));
      }
    } else {
      valueLong = Long.parseLong(value);
    }
  } catch (final NumberFormatException e) {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value), e);
  }

  if (returnType.isAssignableFrom(Long.class)) {
    return returnType.cast(valueLong);
  } else if (returnType.isAssignableFrom(Byte.class)) {
    if (valueLong >= Byte.MIN_VALUE && valueLong <= Byte.MAX_VALUE) {
      return returnType.cast(valueLong.byteValue());
    } else {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_UNCONVERTIBLE_TO_VALUE_TYPE.addContent(value,
          returnType));
    }
  } else if (returnType.isAssignableFrom(Short.class)) {
    if (valueLong >= Short.MIN_VALUE && valueLong <= Short.MAX_VALUE) {
      return returnType.cast(valueLong.shortValue());
    } else {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_UNCONVERTIBLE_TO_VALUE_TYPE.addContent(value,
          returnType));
    }
  } else if (returnType.isAssignableFrom(Integer.class)) {
    if (valueLong >= Integer.MIN_VALUE && valueLong <= Integer.MAX_VALUE) {
      return returnType.cast(valueLong.intValue());
    } else {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_UNCONVERTIBLE_TO_VALUE_TYPE.addContent(value,
          returnType));
    }
  } else if (returnType.isAssignableFrom(BigInteger.class)) {
    return returnType.cast(BigInteger.valueOf(valueLong));
  } else {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_TYPE_NOT_SUPPORTED.addContent(returnType));
  }
}
 
Example 8
Source File: EdmBinary.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private static boolean validateLiteral(final String value, final EdmLiteralKind literalKind) {
  return literalKind == EdmLiteralKind.URI ?
      value.matches("(?:X|binary)'(?:\\p{XDigit}{2})*'") : Base64.isBase64(value);
}
 
Example 9
Source File: EdmDouble.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> T internalValueOfString(final String value, final EdmLiteralKind literalKind, final EdmFacets facets,
    final Class<T> returnType) throws EdmSimpleTypeException {
  String valueString = value;
  Double result = null;
  // Handle special values first.
  if ("-INF".equals(value)) {
    result = Double.NEGATIVE_INFINITY;
  } else if ("INF".equals(value)) {
    result = Double.POSITIVE_INFINITY;
  } else if ("NaN".equals(value)) {
    result = Double.NaN;
  } else {
    // Now only "normal" numbers remain.
    final Matcher matcher = PATTERN.matcher(value);
    if (!matcher.matches()
        || (literalKind == EdmLiteralKind.URI) == (matcher.group(1) == null)) {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value));
    }

    if (literalKind == EdmLiteralKind.URI) {
      valueString = value.substring(0, value.length() - 1);
    }

    // The number format is checked above, so we don't have to catch NumberFormatException.
    result = Double.valueOf(valueString);

    // "Real" infinite values have been treated already above, so we can throw an exception
    // if the conversion to a float results in an infinite value.
    if (result.isInfinite()) {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value));
    }
  }

  if (returnType.isAssignableFrom(Double.class)) {
    return returnType.cast(result);
  } else if (returnType.isAssignableFrom(Float.class)) {
    if (result.floatValue() == result) {
      return returnType.cast(result.floatValue());
    } else {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_UNCONVERTIBLE_TO_VALUE_TYPE.addContent(value,
          returnType));
    }
  } else if (result.isInfinite() || result.isNaN()) {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_UNCONVERTIBLE_TO_VALUE_TYPE.addContent(value,
        returnType));
  } else {
    try {
      final BigDecimal valueBigDecimal = new BigDecimal(valueString);
      if (returnType.isAssignableFrom(BigDecimal.class)) {
        return returnType.cast(valueBigDecimal);
      } else if (returnType.isAssignableFrom(Long.class)) {
        return returnType.cast(valueBigDecimal.longValueExact());
      } else if (returnType.isAssignableFrom(Integer.class)) {
        return returnType.cast(valueBigDecimal.intValueExact());
      } else if (returnType.isAssignableFrom(Short.class)) {
        return returnType.cast(valueBigDecimal.shortValueExact());
      } else if (returnType.isAssignableFrom(Byte.class)) {
        return returnType.cast(valueBigDecimal.byteValueExact());
      } else {
        throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_TYPE_NOT_SUPPORTED.addContent(returnType));
      }

    } catch (final ArithmeticException e) {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_UNCONVERTIBLE_TO_VALUE_TYPE.addContent(value,
          returnType), e);
    }
  }
}
 
Example 10
Source File: EdmDecimal.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private static boolean validateLiteral(final String value, final EdmLiteralKind literalKind) {
  final Matcher matcher = PATTERN.matcher(value);
  return matcher.matches()
      && (literalKind == EdmLiteralKind.URI) != (matcher.group(3) == null);
}
 
Example 11
Source File: EdmDecimal.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> T internalValueOfString(final String value, final EdmLiteralKind literalKind, final EdmFacets facets,
    final Class<T> returnType) throws EdmSimpleTypeException {
  if (!validateLiteral(value, literalKind)) {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value));
  }
  if (!validatePrecisionAndScale(value, facets)) {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_FACETS_NOT_MATCHED.addContent(value, facets));
  }

  final BigDecimal valueBigDecimal = new BigDecimal(
      literalKind == EdmLiteralKind.URI ? value.substring(0, value.length() - 1) : value);

  if (returnType.isAssignableFrom(BigDecimal.class)) {
    return returnType.cast(valueBigDecimal);
  } else if (returnType.isAssignableFrom(Double.class)) {
    if (BigDecimal.valueOf(valueBigDecimal.doubleValue()).compareTo(valueBigDecimal) == 0) {
      return returnType.cast(valueBigDecimal.doubleValue());
    } else {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_UNCONVERTIBLE_TO_VALUE_TYPE.addContent(value,
          returnType));
    }
  } else if (returnType.isAssignableFrom(Float.class)) {
    if (BigDecimal.valueOf(valueBigDecimal.floatValue()).compareTo(valueBigDecimal) == 0) {
      return returnType.cast(valueBigDecimal.floatValue());
    } else {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_UNCONVERTIBLE_TO_VALUE_TYPE.addContent(value,
          returnType));
    }
  } else {
    try {
      if (returnType.isAssignableFrom(BigInteger.class)) {
        return returnType.cast(valueBigDecimal.toBigIntegerExact());
      } else if (returnType.isAssignableFrom(Long.class)) {
        return returnType.cast(valueBigDecimal.longValueExact());
      } else if (returnType.isAssignableFrom(Integer.class)) {
        return returnType.cast(valueBigDecimal.intValueExact());
      } else if (returnType.isAssignableFrom(Short.class)) {
        return returnType.cast(valueBigDecimal.shortValueExact());
      } else if (returnType.isAssignableFrom(Byte.class)) {
        return returnType.cast(valueBigDecimal.byteValueExact());
      } else {
        throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_TYPE_NOT_SUPPORTED.addContent(returnType));
      }
    } catch (final ArithmeticException e) {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_UNCONVERTIBLE_TO_VALUE_TYPE.addContent(value,
          returnType), e);
    }
  }
}
 
Example 12
Source File: EdmTime.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> T internalValueOfString(final String value, final EdmLiteralKind literalKind, final EdmFacets facets,
    final Class<T> returnType) throws EdmSimpleTypeException {

  //OLINGO-883 prefix is case insensitve so we need to check with lower case if we want to use startsWith()
  if (literalKind == EdmLiteralKind.URI
      && (value.length() <= 6 || !value.toLowerCase().startsWith("time'") || !value.endsWith("'"))) {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value));
  }

  final Matcher matcher = PATTERN.matcher(
      literalKind == EdmLiteralKind.URI ? value.substring(5, value.length() - 1) : value);
  if (!matcher.matches()
      || (matcher.group(1) == null && matcher.group(2) == null && matcher.group(3) == null 
      && matcher.group(4) == null && matcher.group(5) == null && matcher.group(6) == null)) {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value));
  }

  Calendar dateTimeValue = Calendar.getInstance();
  dateTimeValue.clear();

  if (matcher.group(1) != null) {
    dateTimeValue.set(Calendar.YEAR, Integer.parseInt(matcher.group(1)));
  }
  if (matcher.group(2) != null) {
    dateTimeValue.set(Calendar.MONTH, Integer.parseInt(matcher.group(2)));
  }
  if (matcher.group(3) != null) {
    dateTimeValue.set(Calendar.DAY_OF_YEAR, Integer.parseInt(matcher.group(3)));
  }
  dateTimeValue.set(Calendar.HOUR_OF_DAY,
      matcher.group(4) == null ? 0 : Integer.parseInt(matcher.group(4)));
  dateTimeValue.set(Calendar.MINUTE,
      matcher.group(5) == null ? 0 : Integer.parseInt(matcher.group(5)));
  dateTimeValue.set(Calendar.SECOND,
      matcher.group(6) == null ? 0 : Integer.parseInt(matcher.group(6)));

  int nanoSeconds = 0;
  if (matcher.group(7) != null) {
    final String decimals = matcher.group(7);
    if (facets == null || facets.getPrecision() == null || facets.getPrecision() >= decimals.length()) {
      nanoSeconds = Integer.parseInt(decimals + "000000000".substring(decimals.length()));
      if (!(returnType.isAssignableFrom(Timestamp.class))) {
        if (nanoSeconds % (1000 * 1000) == 0) {
          dateTimeValue.set(Calendar.MILLISECOND, nanoSeconds / (1000 * 1000));
        } else {
          throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value));
        }
      }
    } else {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_FACETS_NOT_MATCHED.addContent(value, facets));
    }
  }

  if (returnType.isAssignableFrom(Calendar.class)) {
    return returnType.cast(dateTimeValue);
  } else if (returnType.isAssignableFrom(Long.class)) {
    return returnType.cast(dateTimeValue.getTimeInMillis());
  } else if (returnType.isAssignableFrom(Date.class)) {
    return returnType.cast(dateTimeValue.getTime());
  } else if (returnType.isAssignableFrom(Time.class)) {
    return returnType.cast(new Time(dateTimeValue.getTimeInMillis()));
  } else if (returnType.isAssignableFrom(Timestamp.class)) {
    Timestamp timestamp = new Timestamp(dateTimeValue.getTimeInMillis());
    timestamp.setNanos(nanoSeconds);
    return returnType.cast(timestamp);
  } else {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_TYPE_NOT_SUPPORTED.addContent(returnType));
  }
}
 
Example 13
Source File: EdmSingle.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> T internalValueOfString(final String value, final EdmLiteralKind literalKind, final EdmFacets facets,
    final Class<T> returnType) throws EdmSimpleTypeException {
  String valueString = value;
  Float result = null;
  // Handle special values first.
  if ("-INF".equals(value)) {
    result = Float.NEGATIVE_INFINITY;
  } else if ("INF".equals(value)) {
    result = Float.POSITIVE_INFINITY;
  } else if ("NaN".equals(value)) {
    result = Float.NaN;
  } else {
    // Now only "normal" numbers remain.
    final Matcher matcher = PATTERN.matcher(value);
    if (!matcher.matches()
        || (literalKind == EdmLiteralKind.URI) == (matcher.group(1) == null)) {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value));
    }

    if (literalKind == EdmLiteralKind.URI) {
      valueString = value.substring(0, value.length() - 1);
    }

    // The number format is checked above, so we don't have to catch NumberFormatException.
    result = Float.valueOf(valueString);
    // "Real" infinite values have been treated already above, so we can throw an exception
    // if the conversion to a float results in an infinite value.
    if (result.isInfinite()) {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value));
    }
  }

  if (returnType.isAssignableFrom(Float.class)) {
    return returnType.cast(result);
  } else if (returnType.isAssignableFrom(Double.class)) {
    if (result.isInfinite() || result.isNaN()) {
      return returnType.cast(result.doubleValue());
    } else {
      return returnType.cast(Double.valueOf(valueString));
    }
  } else if (result.isInfinite() || result.isNaN()) {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_UNCONVERTIBLE_TO_VALUE_TYPE.addContent(value,
        returnType));
  } else {
    try {
      final BigDecimal valueBigDecimal = new BigDecimal(valueString);
      if (returnType.isAssignableFrom(BigDecimal.class)) {
        return returnType.cast(valueBigDecimal);
      } else if (returnType.isAssignableFrom(Long.class)) {
        return returnType.cast(valueBigDecimal.longValueExact());
      } else if (returnType.isAssignableFrom(Integer.class)) {
        return returnType.cast(valueBigDecimal.intValueExact());
      } else if (returnType.isAssignableFrom(Short.class)) {
        return returnType.cast(valueBigDecimal.shortValueExact());
      } else if (returnType.isAssignableFrom(Byte.class)) {
        return returnType.cast(valueBigDecimal.byteValueExact());
      } else {
        throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_TYPE_NOT_SUPPORTED.addContent(returnType));
      }

    } catch (final ArithmeticException e) {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_UNCONVERTIBLE_TO_VALUE_TYPE.addContent(value,
          returnType), e);
    }
  }
}