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.Iterator;
034: import java.util.List;
035: import java.util.logging.Level;
036: import java.util.logging.Logger;
037:
038: /**
039: * Resolves properties based on lists.
040: */
041: public class ListELResolver extends ELResolver {
042: private final static Logger log = Logger
043: .getLogger(ListELResolver.class.getName());
044:
045: private final boolean _isReadOnly;
046:
047: public ListELResolver() {
048: _isReadOnly = false;
049: }
050:
051: public ListELResolver(boolean isReadOnly) {
052: _isReadOnly = true;
053: }
054:
055: @Override
056: public Class<?> getCommonPropertyType(ELContext context, Object base) {
057: if (base == null)
058: return null;
059: else if (base instanceof List)
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 List) {
069: context.setPropertyResolved(true);
070:
071: return null;
072: } else {
073: return null;
074: }
075: }
076:
077: @Override
078: public Class<?> getType(ELContext context, Object base,
079: Object property) {
080: if (context == null)
081: throw new NullPointerException();
082:
083: if (base instanceof List) {
084: context.setPropertyResolved(true);
085:
086: return Object.class;
087: } else
088: return null;
089: }
090:
091: @Override
092: public Object getValue(ELContext context, Object base,
093: Object property) {
094: if (base instanceof List) {
095: List list = (List) base;
096:
097: context.setPropertyResolved(true);
098:
099: int index = 0;
100:
101: if (property instanceof Number)
102: index = ((Number) property).intValue();
103: else if (property instanceof String) {
104: try {
105: index = Integer.parseInt((String) property);
106: } catch (Exception e) {
107: throw new ELException("can't convert '" + property
108: + "' to long.");
109: }
110: } else {
111: throw new ELException("can't convert '" + property
112: + "' to long.");
113: }
114:
115: if (0 <= index && index < list.size())
116: return list.get(index);
117: else
118: throw new PropertyNotFoundException("List.getIndex '"
119: + index
120: + "' is an invalid index for list of length '"
121: + list.size() + "'");
122: } else {
123: return null;
124: }
125: }
126:
127: @Override
128: public boolean isReadOnly(ELContext context, Object base,
129: Object property) {
130: if (base instanceof List) {
131: context.setPropertyResolved(true);
132:
133: return _isReadOnly;
134: } else
135: return false;
136: }
137:
138: @Override
139: public void setValue(ELContext context, Object base,
140: Object property, Object value) {
141: if (base instanceof List) {
142: List list = (List) base;
143:
144: context.setPropertyResolved(true);
145:
146: int index = 0;
147:
148: if (property instanceof Number)
149: index = ((Number) property).intValue();
150: else if (property instanceof String) {
151: try {
152: index = Integer.parseInt((String) property);
153: } catch (Exception e) {
154: log.log(Level.FINE, e.toString(), e);
155: }
156: }
157:
158: if (index < 0 || list.size() < index)
159: throw new PropertyNotFoundException("List.setValue '"
160: + index
161: + "' is an invalid index for list of length '"
162: + list.size() + "'");
163: list.set(index, value);
164:
165: }
166: }
167: }
|