This is an automated email from the ASF dual-hosted git repository.
ddekany pushed a commit to branch FREEMARKER-35
in repository https://gitbox.apache.org/repos/asf/freemarker.git
The following commit(s) were added to refs/heads/FREEMARKER-35 by this push:
new 7c6b816 [FREEMARKER-35] Formatting code cleanup. Added markup output
support to temporal formatting.
7c6b816 is described below
commit 7c6b8163ca98c8f77e16697e3c23e6419ecf7311
Author: ddekany <[email protected]>
AuthorDate: Tue Nov 2 22:20:58 2021 +0100
[FREEMARKER-35] Formatting code cleanup. Added markup output support to
temporal formatting.
---
.../freemarker/core/BuiltInsForMultipleTypes.java | 40 +++++-------
src/main/java/freemarker/core/Environment.java | 76 ++++++++++++++++++----
src/main/java/freemarker/core/EvalUtil.java | 50 ++------------
.../ISOLikeTemplateTemporalTemporalFormat.java | 2 +-
.../core/JavaTemplateTemporalFormat.java | 2 +-
.../freemarker/core/TemplateTemporalFormat.java | 16 ++++-
.../core/ToStringTemplateTemporalFormat.java | 2 +-
7 files changed, 108 insertions(+), 80 deletions(-)
diff --git a/src/main/java/freemarker/core/BuiltInsForMultipleTypes.java
b/src/main/java/freemarker/core/BuiltInsForMultipleTypes.java
index 8f15548..c3f67ec 100644
--- a/src/main/java/freemarker/core/BuiltInsForMultipleTypes.java
+++ b/src/main/java/freemarker/core/BuiltInsForMultipleTypes.java
@@ -629,10 +629,10 @@ class BuiltInsForMultipleTypes {
return formatWith(key);
}
- private TemplateModel formatWith(String key)
+ private TemplateModel formatWith(String formatString)
throws TemplateModelException {
try {
- return new
SimpleScalar(env.formatTemporalToPlainText(temporalModel, key, target,
stringBI.this, true));
+ return new
SimpleScalar(env.formatTemporalToPlainText(temporalModel, formatString, target,
stringBI.this, true));
} catch (TemplateException e) {
// `e` should always be a TemplateModelException here, but
to be sure:
throw _CoreAPI.ensureIsTemplateModelException("Failed to
format value", e);
@@ -646,14 +646,10 @@ class BuiltInsForMultipleTypes {
throw new BugException();
}
try {
- cachedValue =
EvalUtil.assertFormatResultNotNull(defaultFormat.format(temporalModel));
- } catch (TemplateValueFormatException e) {
- try {
- throw
_MessageUtil.newCantFormatTemporalException(defaultFormat, temporalModel,
target, e, true);
- } catch (TemplateException e2) {
- // `e` should always be a TemplateModelException
here, but to be sure:
- throw
_CoreAPI.ensureIsTemplateModelException("Failed to format date/time/datetime",
e2);
- }
+ cachedValue =
Environment.formatTemporalToPlainText(temporalModel, target, defaultFormat,
true);
+ } catch (TemplateException e) {
+ // `e` should always be a TemplateModelException here,
but to be sure:
+ throw _CoreAPI.ensureIsTemplateModelException("Failed
to format temporal value", e);
}
}
return cachedValue;
@@ -700,10 +696,10 @@ class BuiltInsForMultipleTypes {
return formatWith(key);
}
- private TemplateModel formatWith(String key)
+ private TemplateModel formatWith(String formatString)
throws TemplateModelException {
try {
- return new
SimpleScalar(env.formatDateToPlainText(dateModel, key, target, stringBI.this,
true));
+ return new
SimpleScalar(env.formatDateToPlainText(dateModel, formatString, target,
stringBI.this, true));
} catch (TemplateException e) {
// `e` should always be a TemplateModelException here, but
to be sure:
throw _CoreAPI.ensureIsTemplateModelException("Failed to
format value", e);
@@ -722,14 +718,10 @@ class BuiltInsForMultipleTypes {
}
}
try {
- cachedValue =
EvalUtil.assertFormatResultNotNull(defaultFormat.formatToPlainText(dateModel));
- } catch (TemplateValueFormatException e) {
- try {
- throw
_MessageUtil.newCantFormatDateException(defaultFormat, target, e, true);
- } catch (TemplateException e2) {
- // `e` should always be a TemplateModelException
here, but to be sure:
- throw
_CoreAPI.ensureIsTemplateModelException("Failed to format date/time/datetime",
e2);
- }
+ cachedValue =
Environment.formatDateToPlainText(dateModel, target, defaultFormat, true);
+ } catch (TemplateException e2) {
+ // `e` should always be a TemplateModelException here,
but to be sure:
+ throw _CoreAPI.ensureIsTemplateModelException("Failed
to format date/time/datetime", e2);
}
}
return cachedValue;
@@ -769,14 +761,18 @@ class BuiltInsForMultipleTypes {
@Override
public Object exec(List args) throws TemplateModelException {
checkMethodArgCount(args, 1);
- return get((String) args.get(0));
+ return formatWith((String) args.get(0));
}
@Override
public TemplateModel get(String key) throws TemplateModelException
{
+ return formatWith(key);
+ }
+
+ private TemplateModel formatWith(String formatString) throws
TemplateModelException {
TemplateNumberFormat format;
try {
- format = env.getTemplateNumberFormat(key, stringBI.this,
true);
+ format = env.getTemplateNumberFormat(formatString,
stringBI.this, true);
} catch (TemplateException e) {
// `e` should always be a TemplateModelException here, but
to be sure:
throw _CoreAPI.ensureIsTemplateModelException("Failed to
get number format", e);
diff --git a/src/main/java/freemarker/core/Environment.java
b/src/main/java/freemarker/core/Environment.java
index 4f0826c..ea90bb0 100644
--- a/src/main/java/freemarker/core/Environment.java
+++ b/src/main/java/freemarker/core/Environment.java
@@ -1454,7 +1454,22 @@ public final class Environment extends Configurable {
}
/**
- * Format number with the default number format.
+ * Format number with the default number format to plain text or markup.
+ *
+ * @param exp
+ * The blamed expression if an error occurs; it's only needed
for better error messages
+ */
+ Object formatNumber(TemplateNumberModel number, Expression exp, boolean
useTempModelExc) throws TemplateException {
+ TemplateNumberFormat format = getTemplateNumberFormat(exp, false);
+ try {
+ return EvalUtil.assertFormatResultNotNull(format.format(number));
+ } catch (TemplateValueFormatException e) {
+ throw _MessageUtil.newCantFormatNumberException(format, exp, e,
false);
+ }
+ }
+
+ /**
+ * Format number with the default number format to plain text.
*
* @param exp
* The blamed expression if an error occurs; it's only needed
for better error messages
@@ -1470,7 +1485,7 @@ public final class Environment extends Configurable {
* @param exp
* The blamed expression if an error occurs; it's only needed
for better error messages
*/
- String formatNumberToPlainText(
+ static String formatNumberToPlainText(
TemplateNumberModel number, TemplateNumberFormat format,
Expression exp,
boolean useTempModelExc)
throws TemplateException {
@@ -1753,20 +1768,35 @@ public final class Environment extends Configurable {
}
/**
+ * Format date with the default format to plain text or markup.
+ *
* @param blamedTdmSourceExpr
* The blamed expression if an error occurs; only used for
error messages.
*/
- String formatDateToPlainText(TemplateDateModel tdm, Expression
blamedTdmSourceExpr,
- boolean useTempModelExc) throws TemplateException {
+ Object formatDate(TemplateDateModel tdm, Expression blamedTdmSourceExpr,
boolean useTempModelExc)
+ throws TemplateException {
TemplateDateFormat format = getTemplateDateFormat(tdm,
blamedTdmSourceExpr, useTempModelExc);
try {
- return
EvalUtil.assertFormatResultNotNull(format.formatToPlainText(tdm));
+ return EvalUtil.assertFormatResultNotNull(format.format(tdm));
} catch (TemplateValueFormatException e) {
throw _MessageUtil.newCantFormatDateException(format,
blamedTdmSourceExpr, e, useTempModelExc);
}
}
/**
+ * Format date with the default format to plain text.
+ *
+ * @param blamedTdmSourceExpr
+ * The blamed expression if an error occurs; only used for
error messages.
+ */
+ String formatDateToPlainText(
+ TemplateDateModel tdm, Expression blamedTdmSourceExpr,
+ boolean useTempModelExc) throws TemplateException {
+ TemplateDateFormat format = getTemplateDateFormat(tdm,
blamedTdmSourceExpr, useTempModelExc);
+ return formatDateToPlainText(tdm, blamedTdmSourceExpr, format,
useTempModelExc);
+ }
+
+ /**
* @param blamedTdmSourceExp
* The blamed expression if an error occurs; only used for
error messages.
* @param blamedFormatterExp
@@ -1779,7 +1809,12 @@ public final class Environment extends Configurable {
formatString, tdm.getDateType(), EvalUtil.modelToDate(tdm,
blamedTdmSourceExp).getClass(),
blamedTdmSourceExp, blamedFormatterExp,
useTempModelExc);
-
+ return formatDateToPlainText(tdm, blamedTdmSourceExp, format,
useTempModelExc);
+ }
+
+ static String formatDateToPlainText(
+ TemplateDateModel tdm, Expression blamedTdmSourceExp,
TemplateDateFormat format,
+ boolean useTempModelExc) throws TemplateException {
try {
return
EvalUtil.assertFormatResultNotNull(format.formatToPlainText(tdm));
} catch (TemplateValueFormatException e) {
@@ -2214,6 +2249,24 @@ public final class Environment extends Configurable {
}
/**
+ * Format temporal with the default format to plain text or markup.
+ *
+ * @param blamedTtmSourceExp
+ * The blamed expression if an error occurs; only used for
error messages.
+ */
+ Object formatTemporal(TemplateTemporalModel ttm, Expression
blamedTtmSourceExp, boolean useTempModelExc)
+ throws TemplateException {
+ TemplateTemporalFormat format = getTemplateTemporalFormat(ttm,
blamedTtmSourceExp, useTempModelExc);
+ try {
+ return EvalUtil.assertFormatResultNotNull(format.format(ttm));
+ } catch (TemplateValueFormatException e) {
+ throw _MessageUtil.newCantFormatTemporalException(format, ttm,
blamedTtmSourceExp, e, useTempModelExc);
+ }
+ }
+
+ /**
+ * Format temporal with the default format to plain text.
+ *
* @param blamedTtmSourceExp
* The blamed expression if an error occurs; only used for
error messages.
*/
@@ -2226,7 +2279,7 @@ public final class Environment extends Configurable {
formatString, ttm,
blamedTtmSourceExp, blamedFormatterSourceExp,
useTempModelExc);
- return Environment.this.formatTemporalToPlainText(ttm,
blamedTtmSourceExp, ttf, true);
+ return formatTemporalToPlainText(ttm, blamedTtmSourceExp, ttf, true);
}
String formatTemporalToPlainText(
@@ -2238,12 +2291,12 @@ public final class Environment extends Configurable {
return formatTemporalToPlainText(ttm, blamedTtmSourceExp, ttf, false);
}
- String formatTemporalToPlainText(
+ static String formatTemporalToPlainText(
TemplateTemporalModel ttm, Expression blamedTtmSourceExp,
TemplateTemporalFormat ttf,
boolean useTempModelExc)
throws TemplateException {
try {
- return EvalUtil.assertFormatResultNotNull(ttf.format(ttm));
+ return
EvalUtil.assertFormatResultNotNull(ttf.formatToPlainText(ttm));
} catch (TemplateValueFormatException e) {
throw _MessageUtil.newCantFormatTemporalException(ttf, ttm,
blamedTtmSourceExp, e, useTempModelExc);
}
@@ -2256,8 +2309,9 @@ public final class Environment extends Configurable {
TemplateTemporalModel ttm, Expression blamedTemporalSourceExp,
boolean useTempModelExc)
throws TemplateException {
return getTemplateTemporalFormat(
- EvalUtil.modelToTemporal(
- ttm, blamedTemporalSourceExp).getClass(),
blamedTemporalSourceExp, useTempModelExc);
+ EvalUtil.modelToTemporal(ttm,
blamedTemporalSourceExp).getClass(),
+ blamedTemporalSourceExp,
+ useTempModelExc);
}
/**
diff --git a/src/main/java/freemarker/core/EvalUtil.java
b/src/main/java/freemarker/core/EvalUtil.java
index 2dbf57e..275c211 100644
--- a/src/main/java/freemarker/core/EvalUtil.java
+++ b/src/main/java/freemarker/core/EvalUtil.java
@@ -392,29 +392,11 @@ class EvalUtil {
TemplateModel tm, Expression exp, boolean
returnNullOnNonCoercableType, String seqTip, Environment env)
throws TemplateException {
if (tm instanceof TemplateNumberModel) {
- TemplateNumberModel tnm = (TemplateNumberModel) tm;
- TemplateNumberFormat format = env.getTemplateNumberFormat(exp,
false);
- try {
- return assertFormatResultNotNull(format.format(tnm));
- } catch (TemplateValueFormatException e) {
- throw _MessageUtil.newCantFormatNumberException(format, exp,
e, false);
- }
+ return env.formatNumber((TemplateNumberModel) tm, exp, false);
} else if (tm instanceof TemplateDateModel) {
- TemplateDateModel tdm = (TemplateDateModel) tm;
- TemplateDateFormat format = env.getTemplateDateFormat(tdm, exp,
false);
- try {
- return assertFormatResultNotNull(format.format(tdm));
- } catch (TemplateValueFormatException e) {
- throw _MessageUtil.newCantFormatDateException(format, exp, e,
false);
- }
+ return env.formatDate((TemplateDateModel) tm, exp, false);
} else if (tm instanceof TemplateTemporalModel) {
- TemplateTemporalModel ttm = (TemplateTemporalModel) tm;
- TemplateTemporalFormat format =
env.getTemplateTemporalFormat(ttm.getAsTemporal().getClass(), exp, false);
- try {
- return assertFormatResultNotNull(format.format(ttm));
- } catch (TemplateValueFormatException e) {
- throw _MessageUtil.newCantFormatTemporalException(format, ttm,
exp, e, false);
- }
+ return env.formatTemporal((TemplateTemporalModel) tm, exp, false);
} else if (tm instanceof TemplateMarkupOutputModel) {
return tm;
} else {
@@ -435,29 +417,11 @@ class EvalUtil {
TemplateModel tm, Expression exp, String seqTip, Environment env)
throws TemplateException {
if (tm instanceof TemplateNumberModel) {
- TemplateNumberModel tnm = (TemplateNumberModel) tm;
- TemplateNumberFormat format = env.getTemplateNumberFormat(exp,
false);
- try {
- return ensureFormatResultString(format.format(tnm), exp, env);
- } catch (TemplateValueFormatException e) {
- throw _MessageUtil.newCantFormatNumberException(format, exp,
e, false);
- }
+ return
ensureFormatResultString(env.formatNumber((TemplateNumberModel) tm, exp,
false), exp, env);
} else if (tm instanceof TemplateDateModel) {
- TemplateDateModel tdm = (TemplateDateModel) tm;
- TemplateDateFormat format = env.getTemplateDateFormat(tdm, exp,
false);
- try {
- return ensureFormatResultString(format.format(tdm), exp, env);
- } catch (TemplateValueFormatException e) {
- throw _MessageUtil.newCantFormatDateException(format, exp, e,
false);
- }
+ return ensureFormatResultString(env.formatDate((TemplateDateModel)
tm, exp, false), exp, env);
} else if (tm instanceof TemplateTemporalModel) {
- TemplateTemporalModel ttm = (TemplateTemporalModel) tm;
- TemplateTemporalFormat format = env.getTemplateTemporalFormat(ttm,
exp, false);
- try {
- return ensureFormatResultString(format.format(ttm), exp, env);
- } catch (TemplateValueFormatException e) {
- throw _MessageUtil.newCantFormatTemporalException(format, ttm,
exp, e, false);
- }
+ return
ensureFormatResultString(env.formatTemporal((TemplateTemporalModel) tm, exp,
false), exp, env);
} else {
return coerceModelToTextualCommon(tm, exp, seqTip, false, false,
env);
}
@@ -570,7 +534,7 @@ class EvalUtil {
TemplateMarkupOutputModel mo = (TemplateMarkupOutputModel)
formatResult;
_ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder(
- "Value was formatted to convert it to string, but the result
was markup of ouput format ",
+ "Value was formatted to convert it to string, but the result
was markup of output format ",
new _DelayedJQuote(mo.getOutputFormat()), ".")
.tip("Use value?string to force formatting to plain text.")
.blame(exp);
diff --git
a/src/main/java/freemarker/core/ISOLikeTemplateTemporalTemporalFormat.java
b/src/main/java/freemarker/core/ISOLikeTemplateTemporalTemporalFormat.java
index fc500e0..82be105 100644
--- a/src/main/java/freemarker/core/ISOLikeTemplateTemporalTemporalFormat.java
+++ b/src/main/java/freemarker/core/ISOLikeTemplateTemporalTemporalFormat.java
@@ -55,7 +55,7 @@ final class ISOLikeTemplateTemporalTemporalFormat extends
TemplateTemporalFormat
}
@Override
- public String format(TemplateTemporalModel tm) throws
TemplateValueFormatException,
+ public String formatToPlainText(TemplateTemporalModel tm) throws
TemplateValueFormatException,
TemplateModelException {
Temporal temporal = TemplateFormatUtil.getNonNullTemporal(tm);
diff --git a/src/main/java/freemarker/core/JavaTemplateTemporalFormat.java
b/src/main/java/freemarker/core/JavaTemplateTemporalFormat.java
index fb5f32d..dbb03be 100644
--- a/src/main/java/freemarker/core/JavaTemplateTemporalFormat.java
+++ b/src/main/java/freemarker/core/JavaTemplateTemporalFormat.java
@@ -134,7 +134,7 @@ class JavaTemplateTemporalFormat extends
TemplateTemporalFormat {
}
@Override
- public String format(TemplateTemporalModel tm) throws
TemplateValueFormatException, TemplateModelException {
+ public String formatToPlainText(TemplateTemporalModel tm) throws
TemplateValueFormatException, TemplateModelException {
DateTimeFormatter dateTimeFormatter = this.dateTimeFormatter;
Temporal temporal = TemplateFormatUtil.getNonNullTemporal(tm);
diff --git a/src/main/java/freemarker/core/TemplateTemporalFormat.java
b/src/main/java/freemarker/core/TemplateTemporalFormat.java
index 39116a6..7b43265 100644
--- a/src/main/java/freemarker/core/TemplateTemporalFormat.java
+++ b/src/main/java/freemarker/core/TemplateTemporalFormat.java
@@ -38,7 +38,21 @@ import freemarker.template.TemplateTemporalModel;
*/
public abstract class TemplateTemporalFormat extends TemplateValueFormat {
- public abstract String format(TemplateTemporalModel temporalModel) throws
TemplateValueFormatException, TemplateModelException;
+ public abstract String formatToPlainText(TemplateTemporalModel
temporalModel) throws TemplateValueFormatException, TemplateModelException;
+
+ /**
+ * Formats the model to markup instead of to plain text if the result
markup will be more than just plain text
+ * escaped, otherwise falls back to formatting to plain text. If the
markup result would be just the result of
+ * {@link #formatToPlainText(TemplateTemporalModel)} escaped, it must
return the {@link String} that
+ * {@link #formatToPlainText(TemplateTemporalModel)} does.
+ *
+ * <p>The implementation in {@link TemplateTemporalFormat} simply calls
{@link #formatToPlainText(TemplateTemporalModel)}.
+ *
+ * @return A {@link String} or a {@link TemplateMarkupOutputModel}; not
{@code null}.
+ */
+ public String format(TemplateTemporalModel temporalModel) throws
TemplateValueFormatException, TemplateModelException {
+ return formatToPlainText(temporalModel);
+ }
/**
* Tells if the same formatter can be used regardless of the desired
locale (so for example after a
diff --git a/src/main/java/freemarker/core/ToStringTemplateTemporalFormat.java
b/src/main/java/freemarker/core/ToStringTemplateTemporalFormat.java
index 4e3dd5b..0ee291c 100644
--- a/src/main/java/freemarker/core/ToStringTemplateTemporalFormat.java
+++ b/src/main/java/freemarker/core/ToStringTemplateTemporalFormat.java
@@ -43,7 +43,7 @@ class ToStringTemplateTemporalFormat extends
TemplateTemporalFormat {
}
@Override
- public String format(TemplateTemporalModel temporalModel) throws
TemplateValueFormatException,
+ public String formatToPlainText(TemplateTemporalModel temporalModel)
throws TemplateValueFormatException,
TemplateModelException {
Temporal temporal =
TemplateFormatUtil.getNonNullTemporal(temporalModel);
// TODO [FREEMARKER-35] This is not right, but for now we mimic what
TemporalUtils did