in src/main/java/org/apache/freemarker/onlinetester/util/DataModelParser.java [130:253]
private static Object parseValue(String value, TimeZone timeZone) throws DataModelParsingException {
// Note: Because we fall back to interpret the input as a literal string value when it doesn't look like
// anything else (like a number, boolean, etc.), it's important to avoid misunderstandings, and throw exception
// in suspicious situations. The user can always quote the string value if we are "too smart". But he will
// be confused about the rules of FreeMarker if what he believes to be a non-string is misinterpreted by this
// parser as a string. Getting sometimes an error and then quoting the string is better than that.
if (value.endsWith(";")) { // Tolerate this habit of Java and JavaScript programmers
value = value.substring(value.length() - 1).trim();
}
if (NUMBER_LIKE.matcher(value).matches()) {
try {
return new BigDecimal(value);
} catch (NumberFormatException e) {
// Maybe it's a ISO 8601 Date/time/datetime
CalendarFieldsToDateConverter calToDateConverter = new TrivialCalendarFieldsToDateConverter();
DateParseException attemptedTemportalPExc = null;
String attemptedTemporalType = null;
final int dashIdx = value.indexOf('-');
final int colonIdx = value.indexOf(':');
if (value.indexOf('T') > 1 || (dashIdx > 1 && colonIdx > dashIdx)) {
try {
return new Timestamp(
DateUtil.parseISO8601DateTime(value, timeZone, calToDateConverter).getTime());
} catch (DateParseException pExc) {
attemptedTemporalType = "date-time";
attemptedTemportalPExc = pExc;
}
} else if (dashIdx > 1) {
try {
return new java.sql.Date(
DateUtil.parseISO8601Date(value, timeZone, calToDateConverter).getTime());
} catch (DateParseException pExc) {
attemptedTemporalType = "date";
attemptedTemportalPExc = pExc;
}
} else if (colonIdx > 1) {
try {
return new Time(
DateUtil.parseISO8601Time(value, timeZone, calToDateConverter).getTime());
} catch (DateParseException pExc) {
attemptedTemporalType = "time";
attemptedTemportalPExc = pExc;
}
}
if (attemptedTemportalPExc == null) {
throw new DataModelParsingException("Malformed number: " + value, e);
} else {
throw new DataModelParsingException(
"Malformed ISO 8601 " + attemptedTemporalType + " (or malformed number): " +
attemptedTemportalPExc.getMessage(), e.getCause());
}
}
} else if (value.startsWith("\"")) {
try {
return JSON_MAPPER.readValue(value, String.class);
} catch (IOException e) {
throw new DataModelParsingException(
"Malformed quoted string (using JSON syntax): " + getMessageWithoutLocation(e),
e);
}
} else if (value.startsWith("\'")) {
throw new DataModelParsingException(
"Malformed quoted string (using JSON syntax): Use \" character for quotation, not \' character.");
} else if (value.startsWith("[")) {
try {
return JSON_MAPPER.readValue(value, List.class);
} catch (IOException e) {
throw new DataModelParsingException(
"Malformed list (using JSON syntax): " + getMessageWithoutLocation(e),
e);
}
} else if (value.startsWith("{")) {
try {
return JSON_MAPPER.readValue(value, LinkedHashMap.class);
} catch (IOException e) {
throw new DataModelParsingException(
"Malformed list (using JSON syntax): " + getMessageWithoutLocation(e),
e);
}
} else if (value.startsWith("<")) {
try {
DocumentBuilder builder = NodeModel.getDocumentBuilderFactory().newDocumentBuilder();
ErrorHandler errorHandler = NodeModel.getErrorHandler();
if (errorHandler != null) builder.setErrorHandler(errorHandler);
final Document doc = builder.parse(new InputSource(new StringReader(value)));
NodeModel.simplify(doc);
return doc;
} catch (SAXException e) {
final String saxMsg = e.getMessage();
throw new DataModelParsingException("Malformed XML: " + (saxMsg != null ? saxMsg : e), e);
} catch (Exception e) {
throw new DataModelParsingException("XML parsing has failed with internal error: " + e, e);
}
} else if (value.equalsIgnoreCase(KEYWORD_TRUE)) {
checkKeywordCase(value, KEYWORD_TRUE);
return Boolean.TRUE;
} else if (value.equalsIgnoreCase(KEYWORD_FALSE)) {
checkKeywordCase(value, KEYWORD_FALSE);
return Boolean.FALSE;
} else if (value.equalsIgnoreCase(KEYWORD_NULL)) {
checkKeywordCase(value, KEYWORD_NULL);
return null;
} else if (value.equalsIgnoreCase(KEYWORD_NAN)) {
checkKeywordCase(value, KEYWORD_NAN);
return Double.NaN;
} else if (value.equalsIgnoreCase(KEYWORD_INFINITY)) {
checkKeywordCase(value, KEYWORD_INFINITY);
return Double.POSITIVE_INFINITY;
} else if (value.equalsIgnoreCase(KEYWORD_POSITIVE_INFINITY)) {
checkKeywordCase(value, KEYWORD_POSITIVE_INFINITY);
return Double.POSITIVE_INFINITY;
} else if (value.equalsIgnoreCase(KEYWORD_NEGATIVE_INFINITY)) {
checkKeywordCase(value, KEYWORD_NEGATIVE_INFINITY);
return Double.NEGATIVE_INFINITY;
} else if (value.length() == 0) {
throw new DataModelParsingException(
"Empty value. (If you indeed wanted a 0 length string, quote it, like \"\".)");
} else {
return value;
}
}