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