| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.seasar.teeda.core.el; |
| 17 | |
|
| 18 | |
import java.lang.reflect.Array; |
| 19 | |
import java.util.Iterator; |
| 20 | |
import java.util.List; |
| 21 | |
import java.util.Map; |
| 22 | |
|
| 23 | |
import javax.faces.component.UIComponent; |
| 24 | |
import javax.faces.el.EvaluationException; |
| 25 | |
import javax.faces.el.PropertyNotFoundException; |
| 26 | |
import javax.faces.el.PropertyResolver; |
| 27 | |
import javax.faces.el.ReferenceSyntaxException; |
| 28 | |
|
| 29 | |
import org.seasar.framework.beans.BeanDesc; |
| 30 | |
import org.seasar.framework.beans.PropertyDesc; |
| 31 | |
import org.seasar.framework.beans.factory.BeanDescFactory; |
| 32 | |
import org.seasar.teeda.core.exception.SetterNotFoundRuntimeException; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | 6 | public class TeedaPropertyResolver extends PropertyResolver { |
| 39 | |
|
| 40 | |
public Object getValue(Object base, Object property) |
| 41 | |
throws EvaluationException, PropertyNotFoundException { |
| 42 | |
|
| 43 | 5 | if (base == null || property == null || property instanceof String |
| 44 | |
&& ((String) property).length() == 0) { |
| 45 | |
|
| 46 | 2 | return null; |
| 47 | |
} |
| 48 | 3 | if (base instanceof Map) { |
| 49 | 2 | return ((Map) base).get(property); |
| 50 | |
} |
| 51 | 1 | if (base instanceof UIComponent) { |
| 52 | 1 | UIComponent baseComponent = (UIComponent) base; |
| 53 | 1 | for (Iterator children = baseComponent.getChildren().iterator(); children |
| 54 | 1 | .hasNext();) { |
| 55 | 0 | UIComponent child = (UIComponent) children.next(); |
| 56 | 0 | if (property.equals(child.getId())) { |
| 57 | 0 | return child; |
| 58 | |
} |
| 59 | |
} |
| 60 | 1 | return null; |
| 61 | |
} |
| 62 | 0 | return getProperty(base, property.toString()); |
| 63 | |
} |
| 64 | |
|
| 65 | |
public Object getValue(Object base, int index) throws EvaluationException, |
| 66 | |
PropertyNotFoundException { |
| 67 | |
|
| 68 | 0 | if (base == null) { |
| 69 | 0 | return null; |
| 70 | |
} |
| 71 | |
try { |
| 72 | 0 | if (base.getClass().isArray()) { |
| 73 | 0 | return Array.get(base, index); |
| 74 | |
} |
| 75 | 0 | if (base instanceof List) { |
| 76 | 0 | return ((List) base).get(index); |
| 77 | |
} |
| 78 | 0 | if (base instanceof UIComponent) { |
| 79 | 0 | return ((UIComponent) base).getChildren().get(index); |
| 80 | |
} |
| 81 | 0 | } catch (IndexOutOfBoundsException e) { |
| 82 | 0 | return null; |
| 83 | 0 | } |
| 84 | 0 | throw new ReferenceSyntaxException("Must be array or List. Bean: " |
| 85 | |
+ base.getClass().getName() + ", index " + index); |
| 86 | |
} |
| 87 | |
|
| 88 | |
public void setValue(Object base, Object property, Object newValue) |
| 89 | |
throws EvaluationException, PropertyNotFoundException { |
| 90 | |
|
| 91 | 1 | if (base == null || property == null || property instanceof String |
| 92 | |
&& ((String) property).length() == 0) { |
| 93 | |
|
| 94 | 0 | return; |
| 95 | |
} |
| 96 | 1 | if (base instanceof Map) { |
| 97 | 0 | ((Map) base).put(property, newValue); |
| 98 | |
|
| 99 | 0 | return; |
| 100 | |
} |
| 101 | 1 | if (base instanceof UIComponent) { |
| 102 | 0 | throw new PropertyNotFoundException( |
| 103 | |
"Bean must not be UIComponent, property: " + property); |
| 104 | |
} |
| 105 | 1 | setProperty(base, property.toString(), newValue); |
| 106 | 0 | } |
| 107 | |
|
| 108 | |
public void setValue(Object base, int index, Object newValue) |
| 109 | |
throws EvaluationException, PropertyNotFoundException { |
| 110 | |
|
| 111 | 0 | if (base == null) { |
| 112 | 0 | return; |
| 113 | |
} |
| 114 | 0 | if (base.getClass().isArray()) { |
| 115 | 0 | Array.set(base, index, newValue); |
| 116 | |
|
| 117 | 0 | return; |
| 118 | |
} |
| 119 | 0 | if (base instanceof List) { |
| 120 | 0 | ((List) base).set(index, newValue); |
| 121 | 0 | return; |
| 122 | |
} |
| 123 | 0 | throw new EvaluationException("Bean must be array or List. Bean: " |
| 124 | |
+ base.getClass().getName() + ", index " + index); |
| 125 | |
} |
| 126 | |
|
| 127 | |
public boolean isReadOnly(Object base, Object property) { |
| 128 | 0 | if (base == null || property == null || property instanceof String |
| 129 | |
&& ((String) property).length() == 0) { |
| 130 | |
|
| 131 | 0 | return false; |
| 132 | |
} |
| 133 | 0 | if (base instanceof Map) { |
| 134 | 0 | return false; |
| 135 | |
} |
| 136 | 0 | if (base instanceof UIComponent) { |
| 137 | 0 | return true; |
| 138 | |
} |
| 139 | 0 | BeanDesc beanDesc = BeanDescFactory.getBeanDesc(base.getClass()); |
| 140 | 0 | String propertyName = property.toString(); |
| 141 | 0 | if (!beanDesc.hasPropertyDesc(propertyName)) { |
| 142 | 0 | return false; |
| 143 | |
} |
| 144 | 0 | PropertyDesc pd = beanDesc.getPropertyDesc(propertyName); |
| 145 | 0 | return !pd.isWritable(); |
| 146 | |
} |
| 147 | |
|
| 148 | |
public boolean isReadOnly(Object base, int index) { |
| 149 | 0 | if (base == null) { |
| 150 | 0 | return false; |
| 151 | |
} |
| 152 | 0 | if (base instanceof List || base.getClass().isArray()) { |
| 153 | 0 | return false; |
| 154 | |
} |
| 155 | 0 | if (base instanceof UIComponent) { |
| 156 | 0 | return true; |
| 157 | |
} |
| 158 | 0 | return false; |
| 159 | |
} |
| 160 | |
|
| 161 | |
public Class getType(Object base, Object property) { |
| 162 | 0 | if (base == null || property == null || property instanceof String |
| 163 | |
&& ((String) property).length() == 0) { |
| 164 | |
|
| 165 | 0 | return null; |
| 166 | |
} |
| 167 | 0 | if (base instanceof Map) { |
| 168 | 0 | Object value = ((Map) base).get(property); |
| 169 | 0 | return (value != null) ? value.getClass() : Object.class; |
| 170 | |
} |
| 171 | 0 | if (base instanceof UIComponent) { |
| 172 | 0 | return UIComponent.class; |
| 173 | |
} |
| 174 | 0 | BeanDesc beanDesc = BeanDescFactory.getBeanDesc(base.getClass()); |
| 175 | 0 | String propertyName = property.toString(); |
| 176 | 0 | if (!beanDesc.hasPropertyDesc(propertyName)) { |
| 177 | 0 | return null; |
| 178 | |
} |
| 179 | 0 | PropertyDesc pd = beanDesc.getPropertyDesc(propertyName); |
| 180 | 0 | return pd.getPropertyType(); |
| 181 | |
} |
| 182 | |
|
| 183 | |
public Class getType(Object base, int index) { |
| 184 | 0 | if (base == null) { |
| 185 | 0 | return null; |
| 186 | |
} |
| 187 | 0 | if (base.getClass().isArray()) { |
| 188 | 0 | return base.getClass().getComponentType(); |
| 189 | |
} |
| 190 | 0 | if (base instanceof List) { |
| 191 | 0 | Object value = ((List) base).get(index); |
| 192 | 0 | return (value != null) ? value.getClass() : Object.class; |
| 193 | |
} |
| 194 | 0 | if (base instanceof UIComponent) { |
| 195 | 0 | return UIComponent.class; |
| 196 | |
} |
| 197 | 0 | return null; |
| 198 | |
} |
| 199 | |
|
| 200 | |
protected void setProperty(Object base, String name, Object newValue) { |
| 201 | 1 | BeanDesc beanDesc = BeanDescFactory.getBeanDesc(base.getClass()); |
| 202 | 1 | PropertyDesc pd = beanDesc.getPropertyDesc(name); |
| 203 | 1 | if (pd.isWritable()) { |
| 204 | 0 | pd.setValue(base, newValue); |
| 205 | |
} else { |
| 206 | 1 | throw new SetterNotFoundRuntimeException(name); |
| 207 | |
} |
| 208 | 0 | } |
| 209 | |
|
| 210 | |
protected Object getProperty(Object base, String name) { |
| 211 | 0 | BeanDesc beanDesc = BeanDescFactory.getBeanDesc(base.getClass()); |
| 212 | 0 | PropertyDesc pd = beanDesc.getPropertyDesc(name); |
| 213 | 0 | if (pd.isReadable()) { |
| 214 | 0 | return pd.getValue(base); |
| 215 | |
} else { |
| 216 | 0 | return null; |
| 217 | |
} |
| 218 | |
} |
| 219 | |
|
| 220 | |
} |