1 17 18 package javax.el; 19 20 import java.beans.FeatureDescriptor ; 21 import java.util.ArrayList ; 22 import java.util.Collections ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 public class MapELResolver extends ELResolver { 29 30 private final static Class UNMODIFIABLE = Collections.unmodifiableMap( 31 new HashMap ()).getClass(); 32 33 private final boolean readOnly; 34 35 public MapELResolver() { 36 this.readOnly = false; 37 } 38 39 public MapELResolver(boolean readOnly) { 40 this.readOnly = readOnly; 41 } 42 43 public Object getValue(ELContext context, Object base, Object property) 44 throws NullPointerException , PropertyNotFoundException, ELException { 45 if (context == null) { 46 throw new NullPointerException (); 47 } 48 49 if (base instanceof Map ) { 50 context.setPropertyResolved(true); 51 return ((Map ) base).get(property); 52 } 53 54 return null; 55 } 56 57 public Class <?> getType(ELContext context, Object base, Object property) 58 throws NullPointerException , PropertyNotFoundException, ELException { 59 if (context == null) { 60 throw new NullPointerException (); 61 } 62 63 if (base instanceof Map ) { 64 context.setPropertyResolved(true); 65 Object obj = ((Map ) base).get(property); 66 return (obj != null) ? obj.getClass() : null; 67 } 68 69 return null; 70 } 71 72 public void setValue(ELContext context, Object base, Object property, 73 Object value) throws NullPointerException , 74 PropertyNotFoundException, PropertyNotWritableException, 75 ELException { 76 if (context == null) { 77 throw new NullPointerException (); 78 } 79 80 if (base instanceof Map ) { 81 context.setPropertyResolved(true); 82 83 if (this.readOnly) { 84 throw new PropertyNotWritableException(message(context, 85 "resolverNotWriteable", new Object [] { base.getClass() 86 .getName() })); 87 } 88 89 try { 90 ((Map ) base).put(property, value); 91 } catch (UnsupportedOperationException e) { 92 throw new PropertyNotWritableException(e); 93 } 94 } 95 } 96 97 public boolean isReadOnly(ELContext context, Object base, Object property) 98 throws NullPointerException , PropertyNotFoundException, ELException { 99 if (context == null) { 100 throw new NullPointerException (); 101 } 102 103 if (base instanceof Map ) { 104 context.setPropertyResolved(true); 105 return this.readOnly || UNMODIFIABLE.equals(base.getClass()); 106 } 107 108 return this.readOnly; 109 } 110 111 public Iterator <FeatureDescriptor > getFeatureDescriptors(ELContext context, Object base) { 112 if (base instanceof Map ) { 113 Iterator itr = ((Map ) base).keySet().iterator(); 114 List <FeatureDescriptor > feats = new ArrayList <FeatureDescriptor >(); 115 Object key; 116 FeatureDescriptor desc; 117 while (itr.hasNext()) { 118 key = itr.next(); 119 desc = new FeatureDescriptor (); 120 desc.setDisplayName(key.toString()); 121 desc.setExpert(false); 122 desc.setHidden(false); 123 desc.setName(key.toString()); 124 desc.setPreferred(true); 125 desc.setValue(RESOLVABLE_AT_DESIGN_TIME, Boolean.FALSE); 126 desc.setValue(TYPE, key.getClass()); 127 feats.add(desc); 128 } 129 return feats.iterator(); 130 } 131 return null; 132 } 133 134 public Class <?> getCommonPropertyType(ELContext context, Object base) { 135 if (base instanceof Map ) { 136 return Object .class; 137 } 138 return null; 139 } 140 141 } 142 | Popular Tags |