Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Handle line endings automatically for files detected as text
# and leave all files detected as binary untouched.
* text=auto

#
# The above will handle all files NOT found below
#
# These files are text and should be normalized (Convert crlf => lf)
*.css text
*.df text
*.java text
*.js text
*.json text
*.jsp text
*.properties text
*.sql text
*.svg text
*.tld text
*.txt text
*.xml text

# These files are binary and should be left untouched
# (binary is a macro for -text -diff)
*.dll binary
*.gif binary
*.ico binary
*.jar binary
*.png binary
*.so binary

25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/target/
/tiles-api/target/
/tiles-core/target/
/tiles-template/target/
/tiles-servlet/target/
/tiles-jsp/target/
/tiles-freemarker/target/
/tiles-velocity/target/
/tiles-el/target/
/tiles-ognl/target/
/tiles-mvel/target/
/tiles-compat/target/
/tiles-extras/target/
/tiles-test-pom/tiles-test-db/target/
/tiles-test-pom/tiles-test-alt/target/
/tiles-test-pom/target/
/tiles-test-pom/tiles-test-common/target/
/tiles-test-pom/tiles-test/target/
/assembly/target/
*~
*.bak
*.swp
*.log
.DS_Store
pom.xml.versionsBackup
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,21 @@
package org.apache.tiles.beans;

import java.io.Serializable;
import org.apache.tiles.awareness.ExpressionAware;
import org.apache.tiles.evaluator.AttributeEvaluator;
import org.apache.tiles.request.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A MenuItem implementation.
* Used to read menu items in definitions.
*
* @version $Rev$ $Date$
*/
public class SimpleMenuItem implements MenuItem, Serializable {
public class SimpleMenuItem implements MenuItem, Serializable, ExpressionAware {

private static final Logger log = LoggerFactory.getLogger(SimpleMenuItem.class);

/**
* The value of the item, i.e. what is really visible to the user.
Expand Down Expand Up @@ -57,6 +64,14 @@ public SimpleMenuItem() {
super();
}

public SimpleMenuItem(String value, String link, String icon, String tooltip) {
super();
this.value = value;
this.link = link;
this.icon = icon;
this.tooltip = tooltip;
}

/**
* Sets the value of the item, i.e. what is really visible to the user.
*
Expand Down Expand Up @@ -132,26 +147,67 @@ public String getTooltip() {
/** {@inheritDoc} */
@Override
public String toString() {
StringBuffer buff = new StringBuffer("SimpleMenuItem[");
StringBuilder buff = new StringBuilder("SimpleMenuItem[");

if (getValue() != null) {
buff.append("value=").append(getValue()).append(", ");
buff.append("value=").append(getValue());
}

if (getLink() != null) {
buff.append("link=").append(getLink()).append(", ");
if (buff.length() > 0) {
buff.append(", ");
}
buff.append("link=").append(getLink());
}

if (getTooltip() != null) {
buff.append("tooltip=").append(getTooltip()).append(", ");
if (buff.length() > 0) {
buff.append(", ");
}
buff.append("tooltip=").append(getTooltip());
}

if (getIcon() != null) {
buff.append("icon=").append(getIcon()).append(", ");
if (buff.length() > 0) {
buff.append(", ");
}
buff.append("icon=").append(getIcon());
}

buff.append("]");
return buff.toString();
}

/**
* Evaluates all values for expressions replacing their contents with the
* result of the evaluation.
*
* In the event of a {@link ClassCastException} the original value is used.
*
* @param eval
* Evaluator instance used to evaluate expressions.
* @param request
* Request object to evaluate expressions with.
*/
@Override
public Object evaluateExpressions(AttributeEvaluator eval, Request request) {
return new SimpleMenuItem(safeEval(eval, request, value),
safeEval(eval, request, link),
safeEval(eval, request, icon),
safeEval(eval, request, tooltip));
}

private String safeEval(AttributeEvaluator eval, Request request, String val) {
if (val == null || val.length() == 0) {
return val;
}
try {
Object res = eval.evaluate(val, request);
return res == null ? null : res.toString();
} catch (Exception ex) {
log.warn("Could not evaluate expressions for SimpleMenuItem: {}", ex.getMessage(), ex);
}
return val;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void testToString() {
item.setTooltip("tooltip");
item.setValue("value");
assertEquals(
"SimpleMenuItem[value=value, link=link, tooltip=tooltip, icon=icon, ]",
"SimpleMenuItem[value=value, link=link, tooltip=tooltip, icon=icon]",
item.toString());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* ExpressionAware.java Jun 12 2015, 07:42
*
* Copyright 2015 The Apache Software Foundation.
*
* Licensed 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.tiles.awareness;

import org.apache.tiles.evaluator.AttributeEvaluator;
import org.apache.tiles.request.Request;


/**
* Provides the ability for extended attributes a mechanism for evaluating
* expressions.
*
* Objects will have {@link #evaluateExpressions(AttributeEvaluator, Request) evaluateExpressions}
* called which leaves the evaluation up to the object in question.
*
* @author Brett Ryan
* @since 3.0.6
*/
public interface ExpressionAware {

/**
* Evaluate supported expressions on object.
*
* Implementors may return any type for view rendering.
*
* @param eval
* Evaluator instance used to evaluate expressions.
* @param request
* Request object to evaluate expressions with.
* @return Model instance with attributes evaluated.
*/
Object evaluateExpressions(AttributeEvaluator eval, Request request);

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@
*/
package org.apache.tiles.evaluator;

import java.util.ArrayList;
import java.util.List;
import org.apache.tiles.Attribute;
import org.apache.tiles.Expression;
import org.apache.tiles.awareness.ExpressionAware;
import org.apache.tiles.request.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Abstract class to link a correct evaluation of an attribute, by evaluating
Expand All @@ -33,7 +38,10 @@
*/
public abstract class AbstractAttributeEvaluator implements AttributeEvaluator {

private static final Logger log = LoggerFactory.getLogger(AbstractAttributeEvaluator.class);

/** {@inheritDoc} */
@Override
public Object evaluate(Attribute attribute, Request request) {
if (attribute == null) {
throw new IllegalArgumentException("The attribute cannot be null");
Expand All @@ -44,9 +52,27 @@ public Object evaluate(Attribute attribute, Request request) {
if (retValue == null) {
Expression expression = attribute.getExpressionObject();
if (expression != null) {
log.debug("Evaluating expression: [attribute={},expression={}]", attribute, expression);
retValue = evaluate(attribute.getExpressionObject()
.getExpression(), request);
}
} else if (retValue instanceof List) {
log.debug("Evaluating iterable for expressions: [attribute={},retValue={}]", attribute, retValue);
List list = (List)retValue;
List newList = new ArrayList(list.size());
for (Object n : list) {
if (n instanceof Attribute) {
Attribute m = (Attribute) n;
if (m.getValue() instanceof ExpressionAware) {
log.debug("Evaluating expression-aware value: [object={}]", n);
m = new Attribute(m);
m.setValue(((ExpressionAware)m.getValue()).evaluateExpressions(this, request));
}
n = m;
}
newList.add(n);
}
retValue = newList;
}

return retValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
*/
package org.apache.tiles.el;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.el.ArrayELResolver;
Expand All @@ -37,10 +39,15 @@
import org.apache.el.ExpressionFactoryImpl;
import org.apache.tiles.Attribute;
import org.apache.tiles.Expression;
import org.apache.tiles.awareness.ExpressionAware;
import org.apache.tiles.evaluator.AttributeEvaluator;
import org.apache.tiles.request.ApplicationContext;
import org.apache.tiles.request.Request;
import org.easymock.EasyMock;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;

/**
* Tests {@link ELAttributeEvaluator}.
*
Expand Down Expand Up @@ -171,6 +178,66 @@ public void testEvaluateString() {
.evaluate(expression, request));
}

public void testEvaluateExpressionAware() {
List<Attribute> list = new ArrayList<Attribute>();
list.add(new Attribute(new Explosion("${requestScope.object1}")));
list.add(new Attribute(new Explosion("${sessionScope.object2}")));
list.add(new Attribute(new Explosion("${applicationScope.object3}")));
list.add(new Attribute(new Explosion("${object1}")));
list.add(new Attribute(new Explosion("${object2}")));
list.add(new Attribute(new Explosion("${object3}")));
list.add(new Attribute(new Explosion("${paulaBean.paula}")));
list.add(new Attribute(new Explosion("String literal")));
Attribute attribute = new Attribute(list);

Object res = evaluator.evaluate(attribute, request);

assertThat("Evaluated instance is not a List.", res, instanceOf(List.class));

List<Attribute> nlist = (List<Attribute>)res;
int i = 0;
assertEquals("The value is not correct", "value", ((Explosion)nlist.get(i++).getValue()).getValue());
assertEquals("The value is not correct", new Integer(1), ((Explosion)nlist.get(i++).getValue()).getValue());
assertEquals("The value is not correct", new Float(2.0), ((Explosion)nlist.get(i++).getValue()).getValue());
assertEquals("The value is not correct", "value", ((Explosion)nlist.get(i++).getValue()).getValue());
assertEquals("The value is not correct", new Integer(1), ((Explosion)nlist.get(i++).getValue()).getValue());
assertEquals("The value is not correct", new Float(2.0), ((Explosion)nlist.get(i++).getValue()).getValue());
assertEquals("The value is not correct", "Brillant", ((Explosion)nlist.get(i++).getValue()).getValue());
assertEquals("The value is not correct", "String literal", ((Explosion)nlist.get(i++).getValue()).getValue());
}

private static final class Explosion implements ExpressionAware {

private final Object value;

public Explosion(Object value) {
this.value = value;
}

public Object getValue() {
return value;
}

@Override
public Object evaluateExpressions(AttributeEvaluator eval, Request request) {
return new Explosion(safeEval(eval, request, value));
}

private Object safeEval(AttributeEvaluator eval, Request request, Object val) {
if (val == null) {
return val;
}
if (val instanceof String) {
String n = (String) val;
if (n.length() == 0) {
return n;
}
return eval.evaluate(n, request);
}
return val;
}
}

/**
* This is The Brillant Paula Bean (sic) just like it was posted to:
* http://thedailywtf.com/Articles/The_Brillant_Paula_Bean.aspx
Expand Down
Loading