freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject [17/36] incubator-freemarker git commit: FREEMARKER-55: use Environment variables instead of pageContext in eval function.
Date Wed, 13 Sep 2017 09:49:18 GMT
FREEMARKER-55: use Environment variables instead of pageContext in eval function.


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

Branch: refs/heads/3
Commit: 6f025c79534117a0f87eb17445e1a76cf06be418
Parents: 9eedd86
Author: Woonsan Ko <woonsan@apache.org>
Authored: Mon Sep 11 14:13:56 2017 -0400
Committer: Woonsan Ko <woonsan@apache.org>
Committed: Mon Sep 11 14:13:56 2017 -0400

----------------------------------------------------------------------
 .../freemarker/spring/model/EvalFunction.java   | 34 +++++++++++++-------
 1 file changed, 22 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/6f025c79/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/EvalFunction.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/EvalFunction.java
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/EvalFunction.java
index a380f19..01c3cc5 100644
--- a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/EvalFunction.java
+++ b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/EvalFunction.java
@@ -80,7 +80,7 @@ public class EvalFunction extends AbstractSpringTemplateFunctionModel {
         final Expression expression = expressionParser.parseExpression(expressionString);
 
         // TODO: cache evaluationContext somewhere in request level....
-        EvaluationContext evaluationContext = createEvaluationContext(env, requestContext);
+        EvaluationContext evaluationContext = createEvaluationContext(env, objectWrapperAndUnwrapper,
requestContext);
 
         final Object result = expression.getValue(evaluationContext);
         return wrapObject(objectWrapperAndUnwrapper, result);
@@ -91,10 +91,11 @@ public class EvalFunction extends AbstractSpringTemplateFunctionModel
{
         return ARGS_LAYOUT;
     }
 
-    private EvaluationContext createEvaluationContext(final Environment env, final RequestContext
requestContext) {
+    private EvaluationContext createEvaluationContext(final Environment env,
+            final ObjectWrapperAndUnwrapper objectWrapperAndUnwrapper, final RequestContext
requestContext) {
         StandardEvaluationContext context = new StandardEvaluationContext();
 
-        context.addPropertyAccessor(new EnvironmentVariablesPropertyAccessor(env));
+        context.addPropertyAccessor(new EnvironmentVariablesPropertyAccessor(env, objectWrapperAndUnwrapper));
         context.addPropertyAccessor(new MapAccessor());
         context.addPropertyAccessor(new EnvironmentAccessor());
         context.setBeanResolver(new BeanFactoryResolver(requestContext.getWebApplicationContext()));
@@ -112,42 +113,51 @@ public class EvalFunction extends AbstractSpringTemplateFunctionModel
{
         return (ConversionService) getRequest().getAttribute(ConversionService.class.getName());
     }
 
-    private static class EnvironmentVariablesPropertyAccessor implements PropertyAccessor
{
+    private class EnvironmentVariablesPropertyAccessor implements PropertyAccessor {
 
         private final Environment env;
+        private final ObjectWrapperAndUnwrapper objectWrapperAndUnwrapper;
 
-        public EnvironmentVariablesPropertyAccessor(final Environment env) {
+        public EnvironmentVariablesPropertyAccessor(final Environment env,
+                final ObjectWrapperAndUnwrapper objectWrapperAndUnwrapper) {
             this.env = env;
+            this.objectWrapperAndUnwrapper = objectWrapperAndUnwrapper;
         }
 
         @Override
         public Class<?>[] getSpecificTargetClasses() {
-            // TODO Auto-generated method stub
             return null;
         }
 
         @Override
         public boolean canRead(EvaluationContext context, Object target, String name) throws
AccessException {
-            // TODO Auto-generated method stub
-            return false;
+            try {
+                return (target == null && env.getVariable(name) != null);
+            } catch (TemplateException e) {
+                throw new AccessException("Can't get environment variable by name, '" + name
+ "'.", e);
+            }
         }
 
         @Override
         public TypedValue read(EvaluationContext context, Object target, String name) throws
AccessException {
-            // TODO Auto-generated method stub
-            return null;
+            try {
+                TemplateModel model = env.getVariable(name);
+                Object value = unwrapObject(objectWrapperAndUnwrapper, model);
+                return new TypedValue(value);
+            } catch (TemplateException e) {
+                throw new AccessException("Can't get environment variable by name, '" + name
+ "'.", e);
+            }
         }
 
         @Override
         public boolean canWrite(EvaluationContext context, Object target, String name) throws
AccessException {
-            // TODO Auto-generated method stub
             return false;
         }
 
         @Override
         public void write(EvaluationContext context, Object target, String name, Object newValue)
                 throws AccessException {
-            // TODO Auto-generated method stub
+            throw new UnsupportedOperationException();
         }
     }
 }


Mime
View raw message