| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.seasar.teeda.core.el.impl.commons; |
| 17 | |
|
| 18 | |
import java.lang.reflect.Method; |
| 19 | |
import java.util.HashMap; |
| 20 | |
import java.util.List; |
| 21 | |
import java.util.Map; |
| 22 | |
|
| 23 | |
import javax.faces.component.UIComponent; |
| 24 | |
import javax.faces.context.FacesContext; |
| 25 | |
import javax.faces.el.EvaluationException; |
| 26 | |
import javax.faces.el.PropertyNotFoundException; |
| 27 | |
import javax.faces.el.ReferenceSyntaxException; |
| 28 | |
import javax.servlet.jsp.el.ELException; |
| 29 | |
import javax.servlet.jsp.el.FunctionMapper; |
| 30 | |
import javax.servlet.jsp.el.VariableResolver; |
| 31 | |
|
| 32 | |
import org.apache.commons.el.ArraySuffix; |
| 33 | |
import org.apache.commons.el.BinaryOperatorExpression; |
| 34 | |
import org.apache.commons.el.ComplexValue; |
| 35 | |
import org.apache.commons.el.ConditionalExpression; |
| 36 | |
import org.apache.commons.el.Expression; |
| 37 | |
import org.apache.commons.el.ExpressionString; |
| 38 | |
import org.apache.commons.el.FunctionInvocation; |
| 39 | |
import org.apache.commons.el.Literal; |
| 40 | |
import org.apache.commons.el.NamedValue; |
| 41 | |
import org.apache.commons.el.PropertySuffix; |
| 42 | |
import org.apache.commons.el.UnaryOperatorExpression; |
| 43 | |
import org.apache.commons.el.ValueSuffix; |
| 44 | |
import org.seasar.teeda.core.el.ExpressionProcessor; |
| 45 | |
import org.seasar.teeda.core.el.Replacer; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
public class CommonsExpressionProcessorImpl implements ExpressionProcessor { |
| 51 | |
|
| 52 | |
private static final long serialVersionUID = 1L; |
| 53 | |
|
| 54 | 1 | private static final Map replacers_ = new HashMap(); |
| 55 | |
|
| 56 | 1 | protected static FunctionMapper mapper_ = new FunctionMapper() { |
| 57 | 1 | public Method resolveFunction(String prefix, String localName) { |
| 58 | 0 | throw new ReferenceSyntaxException(); |
| 59 | |
} |
| 60 | |
}; |
| 61 | |
|
| 62 | 82 | public CommonsExpressionProcessorImpl() { |
| 63 | 82 | init(); |
| 64 | 82 | } |
| 65 | |
|
| 66 | |
public void init() { |
| 67 | 90 | replacers_.put(Object.class, new ObjectReplacer(this)); |
| 68 | 82 | replacers_.put(BinaryOperatorExpression.class, |
| 69 | |
new BinaryOperatorExpressionReplacer(this)); |
| 70 | 82 | replacers_.put(UnaryOperatorExpression.class, |
| 71 | |
new UnaryOperatorExpressionReplacer(this)); |
| 72 | 82 | replacers_.put(FunctionInvocation.class, |
| 73 | |
new IgnorerableExpressionReplacer(this)); |
| 74 | 82 | replacers_.put(Literal.class, new IgnorerableExpressionReplacer(this)); |
| 75 | 82 | replacers_.put(NamedValue.class, |
| 76 | |
new IgnorerableExpressionReplacer(this)); |
| 77 | 82 | replacers_.put(ExpressionString.class, new ExpressionStringReplacer( |
| 78 | |
this)); |
| 79 | 82 | replacers_.put(ComplexValue.class, new ComplexValueReplacer(this)); |
| 80 | 82 | } |
| 81 | |
|
| 82 | |
public void processExpression(Object expression, Class type) { |
| 83 | 190 | Replacer replacer = (Replacer) replacers_.get(type); |
| 84 | 190 | if (replacer != null) { |
| 85 | 174 | replacer.replace(expression); |
| 86 | |
} |
| 87 | 189 | } |
| 88 | |
|
| 89 | |
public Object evaluate(FacesContext context, Object expression) |
| 90 | |
throws EvaluationException { |
| 91 | |
try { |
| 92 | 100 | VariableResolver resolver = new ELVariableResolver(context); |
| 93 | 100 | if (expression instanceof Expression) { |
| 94 | 96 | return ((Expression) expression).evaluate(resolver, mapper_, |
| 95 | |
CommonsElLogger.getLogger()); |
| 96 | 4 | } else if (expression instanceof ExpressionString) { |
| 97 | 4 | return ((ExpressionString) expression).evaluate(resolver, |
| 98 | |
mapper_, CommonsElLogger.getLogger()); |
| 99 | |
} |
| 100 | 0 | } catch (ELException e) { |
| 101 | 0 | throw new EvaluationException(e); |
| 102 | 0 | } |
| 103 | 0 | return null; |
| 104 | |
} |
| 105 | |
|
| 106 | |
public Integer toIndex(Object base, Object index) |
| 107 | |
throws ReferenceSyntaxException { |
| 108 | 49 | Integer indexValue = null; |
| 109 | 49 | if (base instanceof List || base.getClass().isArray()) { |
| 110 | 7 | indexValue = CoercionsUtil.coerceToInteger(index); |
| 111 | 7 | if (indexValue == null) { |
| 112 | 1 | throw new ReferenceSyntaxException(); |
| 113 | |
} |
| 114 | 42 | } else if (base instanceof UIComponent) { |
| 115 | 1 | indexValue = CoercionsUtil.coerceToInteger(index); |
| 116 | |
} |
| 117 | 48 | return indexValue; |
| 118 | |
} |
| 119 | |
|
| 120 | |
public Object resolveBase(FacesContext context, Object expression) |
| 121 | |
throws ReferenceSyntaxException, PropertyNotFoundException { |
| 122 | 35 | while (expression instanceof ConditionalExpression) { |
| 123 | 0 | ConditionalExpression conditionalExpression = ((ConditionalExpression) expression); |
| 124 | 0 | Object value = evaluate(context, conditionalExpression |
| 125 | |
.getCondition()); |
| 126 | 0 | boolean condition = CoercionsUtil.coerceToPrimitiveBoolean(value); |
| 127 | 0 | expression = (condition) ? conditionalExpression.getTrueBranch() |
| 128 | |
: conditionalExpression.getFalseBranch(); |
| 129 | |
} |
| 130 | 35 | if (expression instanceof NamedValue) { |
| 131 | 15 | return ((NamedValue) expression).getName(); |
| 132 | |
} |
| 133 | 20 | if (!(expression instanceof ComplexValue)) { |
| 134 | 3 | return null; |
| 135 | |
} |
| 136 | 17 | ComplexValue complexValue = (ComplexValue) expression; |
| 137 | 17 | Object base = evaluate(context, complexValue.getPrefix()); |
| 138 | 17 | VariableResolver resolver = new ELVariableResolver(context); |
| 139 | 17 | if (base == null) { |
| 140 | 0 | throw new PropertyNotFoundException("Base is null: " |
| 141 | |
+ complexValue.getPrefix().getExpressionString()); |
| 142 | |
} |
| 143 | |
|
| 144 | 17 | List suffixes = complexValue.getSuffixes(); |
| 145 | 17 | int max = suffixes.size() - 1; |
| 146 | 18 | for (int i = 0; i < max; i++) { |
| 147 | 1 | ValueSuffix suffix = (ValueSuffix) suffixes.get(i); |
| 148 | |
try { |
| 149 | 1 | base = suffix.evaluate(base, resolver, mapper_, CommonsElLogger |
| 150 | |
.getLogger()); |
| 151 | 0 | } catch (ELException e) { |
| 152 | 0 | throw new EvaluationException(e); |
| 153 | 1 | } |
| 154 | 1 | if (base == null) { |
| 155 | 0 | throw new PropertyNotFoundException("Base is null: " |
| 156 | |
+ suffix.getExpressionString()); |
| 157 | |
} |
| 158 | |
} |
| 159 | 17 | ArraySuffix arraySuffix = (ArraySuffix) suffixes.get(max); |
| 160 | 17 | Expression arraySuffixIndex = arraySuffix.getIndex(); |
| 161 | |
Object index; |
| 162 | 17 | if (arraySuffixIndex != null) { |
| 163 | 1 | index = evaluate(context, arraySuffixIndex); |
| 164 | 1 | if (index == null) { |
| 165 | 0 | throw new PropertyNotFoundException("Index is null: " |
| 166 | |
+ arraySuffixIndex.getExpressionString()); |
| 167 | |
} |
| 168 | |
} else { |
| 169 | 16 | index = ((PropertySuffix) arraySuffix).getName(); |
| 170 | |
} |
| 171 | 17 | return new Object[] { base, index }; |
| 172 | |
} |
| 173 | |
|
| 174 | |
public Object getCoercedObject(Object newValue, Class type) |
| 175 | |
throws EvaluationException { |
| 176 | 1 | return CoercionsUtil.coerce(newValue, type); |
| 177 | |
} |
| 178 | |
} |