freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject incubator-freemarker git commit: Added public formatToPlainText and formatToPlainTextOrMarkup to Environment.
Date Wed, 13 Sep 2017 18:24:30 GMT
Repository: incubator-freemarker
Updated Branches:
  refs/heads/3 5f6a9174d -> 742ed4fee


Added public formatToPlainText and formatToPlainTextOrMarkup to Environment.


Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/742ed4fe
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/742ed4fe
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/742ed4fe

Branch: refs/heads/3
Commit: 742ed4feeba42457f693c7e1ff9a86ffe2196335
Parents: 5f6a917
Author: ddekany <ddekany@apache.org>
Authored: Wed Sep 13 20:05:21 2017 +0200
Committer: ddekany <ddekany@apache.org>
Committed: Wed Sep 13 20:05:21 2017 +0200

----------------------------------------------------------------------
 .../freemarker/core/ASTDollarInterpolation.java |  2 +-
 .../freemarker/core/ASTExpAddOrConcat.java      |  4 +--
 .../apache/freemarker/core/ASTExpression.java   |  8 +++---
 .../core/BuiltInForLegacyEscaping.java          |  2 +-
 .../core/BuiltInsForOutputFormatRelated.java    |  2 +-
 .../freemarker/core/BuiltInsForSequences.java   |  2 +-
 .../org/apache/freemarker/core/Environment.java | 28 ++++++++++++++++++++
 .../org/apache/freemarker/core/_EvalUtils.java  | 12 ++++-----
 8 files changed, 44 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/742ed4fe/freemarker-core/src/main/java/org/apache/freemarker/core/ASTDollarInterpolation.java
----------------------------------------------------------------------
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/ASTDollarInterpolation.java
b/freemarker-core/src/main/java/org/apache/freemarker/core/ASTDollarInterpolation.java
index 80d7f6a..5dc66c1 100644
--- a/freemarker-core/src/main/java/org/apache/freemarker/core/ASTDollarInterpolation.java
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/ASTDollarInterpolation.java
@@ -95,7 +95,7 @@ final class ASTDollarInterpolation extends ASTInterpolation {
 
     @Override
     protected Object calculateInterpolatedStringOrMarkup(Environment env) throws TemplateException
{
-        return _EvalUtils.coerceModelToStringOrMarkup(escapedExpression.eval(env), escapedExpression,
null, env);
+        return _EvalUtils.coerceModelToPlainTextOrMarkup(escapedExpression.eval(env), escapedExpression,
null, env);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/742ed4fe/freemarker-core/src/main/java/org/apache/freemarker/core/ASTExpAddOrConcat.java
----------------------------------------------------------------------
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/ASTExpAddOrConcat.java
b/freemarker-core/src/main/java/org/apache/freemarker/core/ASTExpAddOrConcat.java
index 6f7670d..230d1a9 100644
--- a/freemarker-core/src/main/java/org/apache/freemarker/core/ASTExpAddOrConcat.java
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/ASTExpAddOrConcat.java
@@ -79,7 +79,7 @@ final class ASTExpAddOrConcat extends ASTExpression {
             // We try string addition first. If hash addition is possible, then instead of
throwing exception
             // we return null and do hash addition instead. (We can't simply give hash addition
a priority, like
             // with sequence addition above, as FTL strings are often also FTL hashes.)
-            Object leftOMOrStr = _EvalUtils.coerceModelToStringOrMarkup(
+            Object leftOMOrStr = _EvalUtils.coerceModelToPlainTextOrMarkup(
                     leftModel, leftExp, /* returnNullOnNonCoercableType = */ hashConcatPossible,
null,
                     env);
             if (leftOMOrStr == null) {
@@ -87,7 +87,7 @@ final class ASTExpAddOrConcat extends ASTExpression {
             }
 
             // Same trick with null return as above.
-            Object rightOMOrStr = _EvalUtils.coerceModelToStringOrMarkup(
+            Object rightOMOrStr = _EvalUtils.coerceModelToPlainTextOrMarkup(
                     rightModel, rightExp, /* returnNullOnNonCoercableType = */ hashConcatPossible,
null,
                     env);
             if (rightOMOrStr == null) {

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/742ed4fe/freemarker-core/src/main/java/org/apache/freemarker/core/ASTExpression.java
----------------------------------------------------------------------
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/ASTExpression.java b/freemarker-core/src/main/java/org/apache/freemarker/core/ASTExpression.java
index 69bb34c..c10beac 100644
--- a/freemarker-core/src/main/java/org/apache/freemarker/core/ASTExpression.java
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/ASTExpression.java
@@ -82,25 +82,25 @@ abstract class ASTExpression extends ASTNode {
     }
 
     Object evalAndCoerceToStringOrMarkup(Environment env) throws TemplateException {
-        return _EvalUtils.coerceModelToStringOrMarkup(eval(env), this, null, env);
+        return _EvalUtils.coerceModelToPlainTextOrMarkup(eval(env), this, null, env);
     }
 
     /**
      * @param seqTip Tip to display if the value type is not coercable, but it's iterable.
      */
     Object evalAndCoerceToStringOrMarkup(Environment env, String seqTip) throws TemplateException
{
-        return _EvalUtils.coerceModelToStringOrMarkup(eval(env), this, seqTip, env);
+        return _EvalUtils.coerceModelToPlainTextOrMarkup(eval(env), this, seqTip, env);
     }
     
     String evalAndCoerceToStringOrUnsupportedMarkup(Environment env) throws TemplateException
{
-        return _EvalUtils.coerceModelToStringOrUnsupportedMarkup(eval(env), this, null, env);
+        return _EvalUtils.coerceModelToPlainTextOrUnsupportedMarkup(eval(env), this, null,
env);
     }
 
     /**
      * @param seqTip Tip to display if the value type is not coercable, but it's iterable.
      */
     String evalAndCoerceToStringOrUnsupportedMarkup(Environment env, String seqTip) throws
TemplateException {
-        return _EvalUtils.coerceModelToStringOrUnsupportedMarkup(eval(env), this, seqTip,
env);
+        return _EvalUtils.coerceModelToPlainTextOrUnsupportedMarkup(eval(env), this, seqTip,
env);
     }
     
     Number evalToNumber(Environment env) throws TemplateException {

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/742ed4fe/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInForLegacyEscaping.java
----------------------------------------------------------------------
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInForLegacyEscaping.java
b/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInForLegacyEscaping.java
index 96304a9..0d7fd43 100644
--- a/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInForLegacyEscaping.java
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInForLegacyEscaping.java
@@ -32,7 +32,7 @@ abstract class BuiltInForLegacyEscaping extends BuiltInBannedWhenAutoEscaping
{
     TemplateModel _eval(Environment env)
     throws TemplateException {
         TemplateModel tm = target.eval(env);
-        Object moOrStr = _EvalUtils.coerceModelToStringOrMarkup(tm, target, null, env);
+        Object moOrStr = _EvalUtils.coerceModelToPlainTextOrMarkup(tm, target, null, env);
         if (moOrStr instanceof String) {
             return calculateResult((String) moOrStr, env);
         } else {

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/742ed4fe/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInsForOutputFormatRelated.java
----------------------------------------------------------------------
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInsForOutputFormatRelated.java
b/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInsForOutputFormatRelated.java
index e1e7b5f..5fa6558 100644
--- a/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInsForOutputFormatRelated.java
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInsForOutputFormatRelated.java
@@ -49,7 +49,7 @@ class BuiltInsForOutputFormatRelated {
         @Override
         protected TemplateModel calculateResult(Environment env) throws TemplateException
{
             TemplateModel lhoTM = target.eval(env);
-            Object lhoMOOrStr = _EvalUtils.coerceModelToStringOrMarkup(lhoTM, target, null,
env);
+            Object lhoMOOrStr = _EvalUtils.coerceModelToPlainTextOrMarkup(lhoTM, target,
null, env);
             MarkupOutputFormat contextOF = outputFormat;
             if (lhoMOOrStr instanceof String) { // TemplateMarkupOutputModel
                 return calculateResult((String) lhoMOOrStr, contextOF, env);

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/742ed4fe/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInsForSequences.java
----------------------------------------------------------------------
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInsForSequences.java
b/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInsForSequences.java
index 6b8be3d..0052a81 100644
--- a/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInsForSequences.java
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/BuiltInsForSequences.java
@@ -210,7 +210,7 @@ class BuiltInsForSequences {
                             hadItem = true;
                         }
                         try {
-                            sb.append(_EvalUtils.coerceModelToStringOrUnsupportedMarkup(item,
null, null, env));
+                            sb.append(_EvalUtils.coerceModelToPlainTextOrUnsupportedMarkup(item,
null, null, env));
                         } catch (TemplateException e) {
                             throw new TemplateException(e,
                                     "\"?", key, "\" failed at index ", idx, " with this error:\n\n",

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/742ed4fe/freemarker-core/src/main/java/org/apache/freemarker/core/Environment.java
----------------------------------------------------------------------
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/Environment.java b/freemarker-core/src/main/java/org/apache/freemarker/core/Environment.java
index 23df374..e6c273e 100644
--- a/freemarker-core/src/main/java/org/apache/freemarker/core/Environment.java
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/Environment.java
@@ -54,6 +54,7 @@ import org.apache.freemarker.core.model.TemplateDirectiveModel;
 import org.apache.freemarker.core.model.TemplateFunctionModel;
 import org.apache.freemarker.core.model.TemplateHashModel;
 import org.apache.freemarker.core.model.TemplateHashModelEx;
+import org.apache.freemarker.core.model.TemplateMarkupOutputModel;
 import org.apache.freemarker.core.model.TemplateModel;
 import org.apache.freemarker.core.model.TemplateModelIterator;
 import org.apache.freemarker.core.model.TemplateModelWithOriginName;
@@ -75,6 +76,7 @@ import org.apache.freemarker.core.valueformat.TemplateDateFormat;
 import org.apache.freemarker.core.valueformat.TemplateDateFormatFactory;
 import org.apache.freemarker.core.valueformat.TemplateNumberFormat;
 import org.apache.freemarker.core.valueformat.TemplateNumberFormatFactory;
+import org.apache.freemarker.core.valueformat.TemplateValueFormat;
 import org.apache.freemarker.core.valueformat.TemplateValueFormatException;
 import org.apache.freemarker.core.valueformat.UndefinedCustomFormatException;
 import org.apache.freemarker.core.valueformat.UnknownDateTypeFormattingUnsupportedException;
@@ -1025,6 +1027,30 @@ public final class Environment extends MutableProcessingConfiguration<Environmen
         return getMainTemplate().getBooleanFormat();
     }
 
+    /**
+     * Converts a value to plain text {@link String}, or to {@link TemplateMarkupOutputModel}
if that's what the
+     * {@link TemplateValueFormat} involved prefer to produce. (Most {@link TemplateValueFormat}
produce plain text,
+     * markup producting formatters are very rare.) This will only convert values that <code>${value}</code>
would too,
+     * for example, it will throw {@link TemplateException} if you try to convert a sequence
or a date whose type is not
+     * known. 
+     * 
+     * @return Never {@code null}
+     * 
+     * @see #formatToPlainText(TemplateModel)
+     */
+    public Object formatToPlainTextOrMarkup(TemplateModel tm)
+            throws TemplateException {
+        return _EvalUtils.coerceModelToPlainTextOrMarkup(tm, null, null, this);
+    }
+    
+    /**
+     * Same as {@link #formatToPlainTextOrMarkup(TemplateModel)}, but always formats to plain
text, even if the
+     * {@link TemplateValueFormat} involved by normally produces markup.
+     */
+    public String formatToPlainText(TemplateModel tm) throws TemplateException {
+        return _EvalUtils.coerceModelToPlainText(tm, null, null, this);
+    }
+    
     String formatBoolean(boolean value, boolean fallbackToTrueFalse) throws TemplateException
{
         TemplateBooleanFormat templateBooleanFormat = getTemplateBooleanFormat();
         if (value) {
@@ -3060,6 +3086,7 @@ public final class Environment extends MutableProcessingConfiguration<Environmen
             return callableDefinition.getArgumentArrayLayout();
         }
 
+        @Override
         boolean isFunction() {
             return false;
         }
@@ -3094,6 +3121,7 @@ public final class Environment extends MutableProcessingConfiguration<Environmen
             return callableDefinition.getArgumentArrayLayout();
         }
 
+        @Override
         boolean isFunction() {
             return true;
         }

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/742ed4fe/freemarker-core/src/main/java/org/apache/freemarker/core/_EvalUtils.java
----------------------------------------------------------------------
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/_EvalUtils.java b/freemarker-core/src/main/java/org/apache/freemarker/core/_EvalUtils.java
index 30b869c..1b59d1e 100644
--- a/freemarker-core/src/main/java/org/apache/freemarker/core/_EvalUtils.java
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/_EvalUtils.java
@@ -341,18 +341,18 @@ public class _EvalUtils {
      * 
      * @return Never {@code null}
      */
-    static Object coerceModelToStringOrMarkup(TemplateModel tm, ASTExpression exp, String
seqTip, Environment env)
+    static Object coerceModelToPlainTextOrMarkup(TemplateModel tm, ASTExpression exp, String
seqTip, Environment env)
             throws TemplateException {
-        return coerceModelToStringOrMarkup(tm, exp, false, seqTip, env);
+        return coerceModelToPlainTextOrMarkup(tm, exp, false, seqTip, env);
     }
     
     /**
      * @return {@code null} if the {@code returnNullOnNonCoercableType} parameter is {@code
true}, and the coercion is
      *         not possible, because of the type is not right for it.
      * 
-     * @see #coerceModelToStringOrMarkup(TemplateModel, ASTExpression, String, Environment)
+     * @see #coerceModelToPlainTextOrMarkup(TemplateModel, ASTExpression, String, Environment)
      */
-    static Object coerceModelToStringOrMarkup(
+    static Object coerceModelToPlainTextOrMarkup(
             TemplateModel tm, ASTExpression exp, boolean returnNullOnNonCoercableType, String
seqTip, Environment env)
             throws TemplateException {
         if (tm instanceof TemplateNumberModel) {
@@ -379,7 +379,7 @@ public class _EvalUtils {
     }
 
     /**
-     * Like {@link #coerceModelToStringOrMarkup(TemplateModel, ASTExpression, String, Environment)},
but gives error
+     * Like {@link #coerceModelToPlainTextOrMarkup(TemplateModel, ASTExpression, String,
Environment)}, but gives error
      * if the result is markup. This is what you normally use where markup results can't
be used.
      *
      * @param seqTip
@@ -387,7 +387,7 @@ public class _EvalUtils {
      * 
      * @return Never {@code null}
      */
-    static String coerceModelToStringOrUnsupportedMarkup(
+    static String coerceModelToPlainTextOrUnsupportedMarkup(
             TemplateModel tm, ASTExpression exp, String seqTip, Environment env)
             throws TemplateException {
         if (tm instanceof TemplateNumberModel) {


Mime
View raw message