001: /*
002: * Copyright (c) 1998-2003 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package javax.el;
031:
032: import java.beans.FeatureDescriptor;
033: import java.util.ArrayList;
034: import java.util.Iterator;
035: import java.util.Map;
036: import java.util.logging.Logger;
037:
038: /**
039: * Resolves properties based on maps.
040: */
041: public class MapELResolver extends ELResolver {
042: private final static Logger log = Logger
043: .getLogger(MapELResolver.class.getName());
044:
045: private final boolean _isReadOnly;
046:
047: public MapELResolver() {
048: _isReadOnly = false;
049: }
050:
051: public MapELResolver(boolean isReadOnly) {
052: _isReadOnly = isReadOnly;
053: }
054:
055: @Override
056: public Class<?> getCommonPropertyType(ELContext context, Object base) {
057: if (base instanceof Map) {
058: context.setPropertyResolved(true);
059:
060: return Object.class;
061: } else
062: return null;
063: }
064:
065: @Override
066: public Iterator<FeatureDescriptor> getFeatureDescriptors(
067: ELContext context, Object base) {
068: if (base instanceof Map) {
069: context.setPropertyResolved(true);
070:
071: ArrayList<FeatureDescriptor> keys = new ArrayList<FeatureDescriptor>();
072:
073: for (Object key : ((Map) base).keySet()) {
074: String name = String.valueOf(key);
075:
076: FeatureDescriptor desc = new FeatureDescriptor();
077: desc.setName(name);
078: desc.setDisplayName(name);
079: desc.setShortDescription("");
080: desc.setExpert(false);
081: desc.setHidden(false);
082: desc.setPreferred(true);
083:
084: if (key == null)
085: desc.setValue(ELResolver.TYPE, null);
086: else
087: desc.setValue(ELResolver.TYPE, key.getClass());
088:
089: desc.setValue(ELResolver.RESOLVABLE_AT_DESIGN_TIME,
090: Boolean.TRUE);
091:
092: keys.add(desc);
093: }
094:
095: return keys.iterator();
096: } else {
097: return null;
098: }
099: }
100:
101: @Override
102: public Class<?> getType(ELContext context, Object base,
103: Object property) {
104: if (context == null)
105: throw new NullPointerException();
106:
107: if (base instanceof Map) {
108: context.setPropertyResolved(true);
109:
110: return Object.class;
111: } else
112: return null;
113: }
114:
115: @Override
116: public Object getValue(ELContext context, Object base,
117: Object property) {
118: if (base instanceof Map && property != null) {
119: Map map = (Map) base;
120:
121: context.setPropertyResolved(true);
122:
123: return map.get(property);
124: } else {
125: return null;
126: }
127: }
128:
129: @Override
130: public boolean isReadOnly(ELContext context, Object base,
131: Object property) {
132: if (base instanceof Map) {
133: context.setPropertyResolved(true);
134:
135: return _isReadOnly;
136: } else
137: return true;
138: }
139:
140: @Override
141: public void setValue(ELContext context, Object base,
142: Object property, Object value) {
143: if (base instanceof Map && property != null) {
144: Map map = (Map) base;
145:
146: context.setPropertyResolved(true);
147:
148: map.put(property, value);
149: }
150: }
151: }
|