if (literalKind == EdmLiteralKind.URI) {
if (value.length() > 16 && value.startsWith("datetimeoffset'") && value.endsWith("'")) {
return internalValueOfString(value.substring(15, value.length() - 1), EdmLiteralKind.DEFAULT, facets,
returnType);
} else {
throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value));
}
}
Calendar dateTimeValue = null;
if (literalKind == EdmLiteralKind.JSON) {
final Matcher matcher = JSON_PATTERN.matcher(value);
if (matcher.matches()) {
long millis;
try {
millis = Long.parseLong(matcher.group(1));
} catch (final NumberFormatException e) {
throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value), e);
}
String timeZone = "GMT";
if (matcher.group(2) != null) {
final int offsetInMinutes = Integer.parseInt(matcher.group(3));
if (offsetInMinutes >= 24 * 60) {
throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value));
}
if (offsetInMinutes != 0) {
timeZone += matcher.group(2) + String.valueOf(offsetInMinutes / 60)
+ ":" + String.format("%02d", offsetInMinutes % 60);
// Convert the local-time milliseconds to UTC.
millis -= (matcher.group(2).equals("+") ? 1 : -1) * offsetInMinutes * 60 * 1000;
}
}
dateTimeValue = Calendar.getInstance(TimeZone.getTimeZone(timeZone));
dateTimeValue.clear();
dateTimeValue.setTimeInMillis(millis);
}
}
if (dateTimeValue == null) {
final Matcher matcher = PATTERN.matcher(value);
if (!matcher.matches()) {
throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value));
}
final String timeZoneOffset =
matcher.group(1) != null && matcher.group(2) != null && !matcher.group(2).matches("[-+]0+:0+") ? matcher
.group(2) : null;
dateTimeValue = Calendar.getInstance(TimeZone.getTimeZone("GMT" + timeZoneOffset));
if (dateTimeValue.get(Calendar.ZONE_OFFSET) == 0 && timeZoneOffset != null) {
throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value));
}
dateTimeValue.clear();
EdmDateTime.parseLiteral(value.substring(0, matcher.group(1) == null ? value.length() : matcher.start(1)),
facets, dateTimeValue);
}
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 {
throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_TYPE_NOT_SUPPORTED.addContent(returnType));
}
}