Skip to content

Commit 41759e8

Browse files
committed
Added support for list attributes to have expressions.
- This will certainly need review as the current implementation breaks current behavior with relation to attributes from list attributes being exposed to the view, while simple attributes are not. - This implementation maps everything to the objects represented type where possible, though due to current requirements elements must remain to be an attribute wrapping the value. - Attribute#getExpression is evaluated and set against a cloned version of Attribute#setValue.
1 parent 515dc95 commit 41759e8

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

tiles-core/src/main/java/org/apache/tiles/evaluator/AbstractAttributeEvaluator.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,18 @@
1818
* specific language governing permissions and limitations
1919
* under the License.
2020
*/
21+
2122
package org.apache.tiles.evaluator;
2223

24+
import java.util.ArrayList;
25+
import java.util.List;
2326
import org.apache.tiles.Attribute;
2427
import org.apache.tiles.Expression;
28+
import org.apache.tiles.ListAttribute;
2529
import org.apache.tiles.request.Request;
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
32+
2633

2734
/**
2835
* Abstract class to link a correct evaluation of an attribute, by evaluating
@@ -33,7 +40,10 @@
3340
*/
3441
public abstract class AbstractAttributeEvaluator implements AttributeEvaluator {
3542

43+
private static final Logger log = LoggerFactory.getLogger(AbstractAttributeEvaluator.class);
44+
3645
/** {@inheritDoc} */
46+
@Override
3747
public Object evaluate(Attribute attribute, Request request) {
3848
if (attribute == null) {
3949
throw new IllegalArgumentException("The attribute cannot be null");
@@ -44,11 +54,49 @@ public Object evaluate(Attribute attribute, Request request) {
4454
if (retValue == null) {
4555
Expression expression = attribute.getExpressionObject();
4656
if (expression != null) {
57+
log.debug("Evaluating expression: [attribute={},expression={}]", attribute, expression);
4758
retValue = evaluate(attribute.getExpressionObject()
4859
.getExpression(), request);
4960
}
61+
} else if (retValue instanceof List) {
62+
log.debug("Evaluating list for expressions: {}", retValue);
63+
retValue = evaluateList((List) retValue, request);
5064
}
5165

66+
log.debug("Returning result: {}", retValue);
5267
return retValue;
5368
}
69+
70+
private List evaluateList(List src, Request request) {
71+
List res = new ArrayList();
72+
for (Object val : src) {
73+
if (val instanceof ListAttribute) {
74+
log.debug("Evaluating list entry (ListAttribute): {}", val);
75+
res.add(evaluateList(((ListAttribute) val).getValue(), request));
76+
} else if (val instanceof Attribute) {
77+
log.debug("Evaluating list entry (Attribute): {}", val);
78+
Attribute att = (Attribute) val;
79+
if (att.getValue() != null) {
80+
res.add(att.getValue() instanceof List
81+
? evaluateList((List) att.getValue(), request)
82+
: att);
83+
} else {
84+
Expression expression = att.getExpressionObject();
85+
if (expression != null) {
86+
log.debug("Evaluating list entry expression: {}", expression);
87+
att = att.clone();
88+
att.setValue(evaluate(expression.getExpression(), request));
89+
res.add(att);
90+
} else {
91+
res.add(att);
92+
}
93+
}
94+
} else {
95+
log.debug("Evaluating list entry ({}): {}", val.getClass(), val);
96+
res.add(val);
97+
}
98+
}
99+
return res;
100+
}
101+
54102
}

0 commit comments

Comments
 (0)