001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.el;
019:
020: import java.beans.FeatureDescriptor;
021: import java.util.ArrayList;
022: import java.util.Collections;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027:
028: public class MapELResolver extends ELResolver {
029:
030: private final static Class UNMODIFIABLE = Collections
031: .unmodifiableMap(new HashMap()).getClass();
032:
033: private final boolean readOnly;
034:
035: public MapELResolver() {
036: this .readOnly = false;
037: }
038:
039: public MapELResolver(boolean readOnly) {
040: this .readOnly = readOnly;
041: }
042:
043: public Object getValue(ELContext context, Object base,
044: Object property) throws NullPointerException,
045: PropertyNotFoundException, ELException {
046: if (context == null) {
047: throw new NullPointerException();
048: }
049:
050: if (base instanceof Map) {
051: context.setPropertyResolved(true);
052: return ((Map) base).get(property);
053: }
054:
055: return null;
056: }
057:
058: public Class<?> getType(ELContext context, Object base,
059: Object property) throws NullPointerException,
060: PropertyNotFoundException, ELException {
061: if (context == null) {
062: throw new NullPointerException();
063: }
064:
065: if (base instanceof Map) {
066: context.setPropertyResolved(true);
067: Object obj = ((Map) base).get(property);
068: return (obj != null) ? obj.getClass() : null;
069: }
070:
071: return null;
072: }
073:
074: public void setValue(ELContext context, Object base,
075: Object property, Object value) throws NullPointerException,
076: PropertyNotFoundException, PropertyNotWritableException,
077: ELException {
078: if (context == null) {
079: throw new NullPointerException();
080: }
081:
082: if (base instanceof Map) {
083: context.setPropertyResolved(true);
084:
085: if (this .readOnly) {
086: throw new PropertyNotWritableException(message(context,
087: "resolverNotWriteable", new Object[] { base
088: .getClass().getName() }));
089: }
090:
091: try {
092: ((Map) base).put(property, value);
093: } catch (UnsupportedOperationException e) {
094: throw new PropertyNotWritableException(e);
095: }
096: }
097: }
098:
099: public boolean isReadOnly(ELContext context, Object base,
100: Object property) throws NullPointerException,
101: PropertyNotFoundException, ELException {
102: if (context == null) {
103: throw new NullPointerException();
104: }
105:
106: if (base instanceof Map) {
107: context.setPropertyResolved(true);
108: return this .readOnly
109: || UNMODIFIABLE.equals(base.getClass());
110: }
111:
112: return this .readOnly;
113: }
114:
115: public Iterator<FeatureDescriptor> getFeatureDescriptors(
116: ELContext context, Object base) {
117: if (base instanceof Map) {
118: Iterator itr = ((Map) base).keySet().iterator();
119: List<FeatureDescriptor> feats = new ArrayList<FeatureDescriptor>();
120: Object key;
121: FeatureDescriptor desc;
122: while (itr.hasNext()) {
123: key = itr.next();
124: desc = new FeatureDescriptor();
125: desc.setDisplayName(key.toString());
126: desc.setExpert(false);
127: desc.setHidden(false);
128: desc.setName(key.toString());
129: desc.setPreferred(true);
130: desc.setValue(RESOLVABLE_AT_DESIGN_TIME, Boolean.FALSE);
131: desc.setValue(TYPE, key.getClass());
132: feats.add(desc);
133: }
134: return feats.iterator();
135: }
136: return null;
137: }
138:
139: public Class<?> getCommonPropertyType(ELContext context, Object base) {
140: if (base instanceof Map) {
141: return Object.class;
142: }
143: return null;
144: }
145:
146: }
|