Repository: struts
Updated Branches:
  refs/heads/master f6876ce6c -> 1bed00d3d


WW-4685 Supports evaluating expressions from tiles definitions as a Struts 
values


Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/1bed00d3
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/1bed00d3
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/1bed00d3

Branch: refs/heads/master
Commit: 1bed00d3d7269bbe11cd3ec1711400e050ef57b4
Parents: f6876ce
Author: Lukasz Lenart <lukaszlen...@apache.org>
Authored: Tue Sep 6 21:33:31 2016 +0200
Committer: Lukasz Lenart <lukaszlen...@apache.org>
Committed: Tue Sep 6 21:33:31 2016 +0200

----------------------------------------------------------------------
 .../struts2/tiles/StrutsAttributeEvaluator.java | 89 ++++++++++++++++++++
 .../tiles/StrutsTilesContainerFactory.java      | 10 ++-
 2 files changed, 97 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/1bed00d3/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsAttributeEvaluator.java
----------------------------------------------------------------------
diff --git 
a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsAttributeEvaluator.java
 
b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsAttributeEvaluator.java
new file mode 100644
index 0000000..287b2a0
--- /dev/null
+++ 
b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsAttributeEvaluator.java
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.struts2.tiles;
+
+import com.opensymphony.xwork2.ActionContext;
+import com.opensymphony.xwork2.LocaleProvider;
+import com.opensymphony.xwork2.TextProvider;
+import com.opensymphony.xwork2.TextProviderFactory;
+import com.opensymphony.xwork2.config.ConfigurationException;
+import com.opensymphony.xwork2.ognl.OgnlUtil;
+import ognl.OgnlException;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.struts2.ServletActionContext;
+import org.apache.tiles.evaluator.AbstractAttributeEvaluator;
+import org.apache.tiles.evaluator.EvaluationException;
+import org.apache.tiles.request.Request;
+import org.apache.tiles.request.servlet.ServletUtil;
+
+import javax.servlet.http.HttpServletRequest;
+
+public class StrutsAttributeEvaluator extends AbstractAttributeEvaluator {
+
+    private static final Logger LOG = 
LogManager.getLogger(StrutsAttributeEvaluator.class);
+
+    @Override
+    public Object evaluate(String expression, Request request) {
+        try {
+            Object result = null;
+
+            HttpServletRequest httpRequest = 
ServletUtil.getServletRequest(request).getRequest();
+            ActionContext ctx = 
ServletActionContext.getActionContext(httpRequest);
+
+            if (ctx == null) {
+                LOG.error("Cannot obtain HttpServletRequest from [{}]", 
request.getClass().getName());
+                throw new ConfigurationException("There is no ActionContext 
for current request!");
+            }
+
+            TextProviderFactory tpf = new TextProviderFactory();
+            ctx.getContainer().inject(tpf);
+            LocaleProvider localeProvider = 
ctx.getContainer().getInstance(LocaleProvider.class);
+
+            TextProvider textProvider = 
tpf.createInstance(ctx.getActionInvocation().getAction().getClass(), 
localeProvider);
+
+            if (textProvider != null) {
+                LOG.debug("Trying find text [{}] using TextProvider {}", 
expression, textProvider);
+                result = textProvider.getText(expression);
+                if (expression.equals(result)) {
+                    LOG.debug("Could not evaluate expression [{}] as a I18N 
key", expression);
+                    result = null;
+                }
+            }
+
+            if (result == null) {
+                OgnlUtil ognlUtil = 
ctx.getContainer().getInstance(OgnlUtil.class);
+                LOG.debug("Trying evaluate expression [{}] using OgnlUtil's 
getValue", expression);
+                result = ognlUtil.getValue(expression, ctx.getContextMap(), 
ctx.getValueStack().getRoot());
+            }
+
+            LOG.debug("Final result of evaluating expression [{}] is: {}", 
expression, result);
+
+            if (result == null) {
+                return expression;
+            } else {
+                return result;
+            }
+        } catch (OgnlException e) {
+            throw new EvaluationException(e);
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/struts/blob/1bed00d3/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java
----------------------------------------------------------------------
diff --git 
a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java
 
b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java
index a8e77ce..cecf94e 100644
--- 
a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java
+++ 
b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java
@@ -77,9 +77,9 @@ import java.util.Set;
 /**
  * Dedicated Struts factory to build Tiles container with support for:
  * - Freemarker
- * - OGNL (as default)
+ * - Struts I18N & ValueStack (as default)
+ * - OGNL
  * - EL
- * - Wildcards
  *
  * If you need additional features create your own listener and factory,
  * you can base on code from Tiles' CompleteAutoloadTilesContainerFactory
@@ -109,6 +109,7 @@ public class StrutsTilesContainerFactory extends 
BasicTilesContainerFactory {
      */
     public static final String OGNL = "OGNL";
     public static final String EL = "EL";
+    public static final String S2 = "S2";
 
     @Override
     public TilesContainer createDecoratedContainer(TilesContainer 
originalContainer, ApplicationContext applicationContext) {
@@ -150,6 +151,7 @@ public class StrutsTilesContainerFactory extends 
BasicTilesContainerFactory {
             LocaleResolver resolver) {
 
         BasicAttributeEvaluatorFactory attributeEvaluatorFactory = new 
BasicAttributeEvaluatorFactory(new DirectAttributeEvaluator());
+        attributeEvaluatorFactory.registerAttributeEvaluator(S2, 
createStrutsEvaluator());
         attributeEvaluatorFactory.registerAttributeEvaluator(OGNL, 
createOGNLEvaluator());
 
         ELAttributeEvaluator elEvaluator = 
createELEvaluator(applicationContext);
@@ -230,6 +232,10 @@ public class StrutsTilesContainerFactory extends 
BasicTilesContainerFactory {
         return evaluator;
     }
 
+    protected StrutsAttributeEvaluator createStrutsEvaluator() {
+        return new StrutsAttributeEvaluator();
+    }
+
     protected OGNLAttributeEvaluator createOGNLEvaluator() {
         try {
             PropertyAccessor objectPropertyAccessor = 
OgnlRuntime.getPropertyAccessor(Object.class);

Reply via email to