1 17 18 package javax.el; 19 20 import java.beans.FeatureDescriptor ; 21 import java.util.ArrayList ; 22 import java.util.Arrays ; 23 import java.util.Collections ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 public class ListELResolver extends ELResolver { 28 29 private final boolean readOnly; 30 31 private final static Class UNMODIFIABLE = Collections.unmodifiableList( 32 new ArrayList ()).getClass(); 33 34 public ListELResolver() { 35 this.readOnly = false; 36 } 37 38 public ListELResolver(boolean readOnly) { 39 this.readOnly = readOnly; 40 } 41 42 public Object getValue(ELContext context, Object base, Object property) 43 throws NullPointerException , PropertyNotFoundException, ELException { 44 if (context == null) { 45 throw new NullPointerException (); 46 } 47 48 if (base instanceof List ) { 49 context.setPropertyResolved(true); 50 List list = (List ) base; 51 int idx = coerce(property); 52 if (idx < 0 || idx >= list.size()) { 53 return null; 54 } 55 return list.get(idx); 56 } 57 58 return null; 59 } 60 61 public Class <?> getType(ELContext context, Object base, Object property) 62 throws NullPointerException , PropertyNotFoundException, ELException { 63 if (context == null) { 64 throw new NullPointerException (); 65 } 66 67 if (base instanceof List ) { 68 context.setPropertyResolved(true); 69 List list = (List ) base; 70 int idx = coerce(property); 71 if (idx < 0 || idx >= list.size()) { 72 return null; 73 } 74 Object obj = list.get(idx); 75 return (obj != null) ? obj.getClass() : null; 76 } 77 78 return null; 79 } 80 81 public void setValue(ELContext context, Object base, Object property, 82 Object value) throws NullPointerException , 83 PropertyNotFoundException, PropertyNotWritableException, 84 ELException { 85 if (context == null) { 86 throw new NullPointerException (); 87 } 88 89 if (base instanceof List ) { 90 context.setPropertyResolved(true); 91 List list = (List ) base; 92 93 if (this.readOnly) { 94 throw new PropertyNotWritableException(message(context, 95 "resolverNotWriteable", new Object [] { base.getClass() 96 .getName() })); 97 } 98 99 int idx = coerce(property); 100 try { 101 list.set(idx, value); 102 } catch (UnsupportedOperationException e) { 103 throw new PropertyNotWritableException(e); 104 } catch (IndexOutOfBoundsException e) { 105 throw new PropertyNotFoundException(e); 106 } 107 } 108 } 109 110 public boolean isReadOnly(ELContext context, Object base, Object property) 111 throws NullPointerException , PropertyNotFoundException, ELException { 112 if (context == null) { 113 throw new NullPointerException (); 114 } 115 116 if (base instanceof List ) { 117 context.setPropertyResolved(true); 118 List list = (List ) base; 119 int idx = coerce(property); 120 if (idx < 0 || idx >= list.size()) { 121 throw new PropertyNotFoundException( 122 new ArrayIndexOutOfBoundsException (idx).getMessage()); 123 } 124 return this.readOnly || UNMODIFIABLE.equals(list.getClass()); 125 } 126 127 return this.readOnly; 128 } 129 130 public Iterator <FeatureDescriptor > getFeatureDescriptors(ELContext context, Object base) { 131 if (base instanceof List ) { 132 FeatureDescriptor [] descs = new FeatureDescriptor [((List ) base).size()]; 133 for (int i = 0; i < descs.length; i++) { 134 descs[i] = new FeatureDescriptor (); 135 descs[i].setDisplayName("["+i+"]"); 136 descs[i].setExpert(false); 137 descs[i].setHidden(false); 138 descs[i].setName(""+i); 139 descs[i].setPreferred(true); 140 descs[i].setValue(RESOLVABLE_AT_DESIGN_TIME, Boolean.FALSE); 141 descs[i].setValue(TYPE, Integer .class); 142 } 143 return Arrays.asList(descs).iterator(); 144 } 145 return null; 146 } 147 148 public Class <?> getCommonPropertyType(ELContext context, Object base) { 149 if (base != null && base instanceof List ) { 150 return Integer .class; 151 } 152 return null; 153 } 154 155 private final static int coerce(Object property) { 156 if (property instanceof Number ) { 157 return ((Number ) property).intValue(); 158 } 159 if (property instanceof Character ) { 160 return ((Character ) property).charValue(); 161 } 162 if (property instanceof Boolean ) { 163 return (((Boolean ) property).booleanValue() ? 1 : 0); 164 } 165 if (property instanceof String ) { 166 return Integer.parseInt((String ) property); 167 } 168 throw new IllegalArgumentException (property != null ? property 169 .toString() : "null"); 170 } 171 } 172 | Popular Tags |