001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * glassfish/bootstrap/legal/CDDLv1.0.txt or
009: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
010: * See the License for the specific language governing
011: * permissions and limitations under the License.
012: *
013: * When distributing Covered Code, include this CDDL
014: * HEADER in each file and include the License file at
015: * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
016: * add the following below this CDDL HEADER, with the
017: * fields enclosed by brackets "[]" replaced with your
018: * own identifying information: Portions Copyright [yyyy]
019: * [name of copyright owner]
020: *
021: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
022: */
023:
024: package javax.el;
025:
026: import java.util.List;
027: import java.util.Iterator;
028: import java.util.Collections;
029: import java.util.ArrayList;
030: import java.beans.FeatureDescriptor;
031:
032: /**
033: * Defines property resolution behavior on instances of {@link java.util.List}.
034: *
035: * <p>This resolver handles base objects of type <code>java.util.List</code>.
036: * It accepts any object as a property and coerces that object into an
037: * integer index into the list. The resulting value is the value in the list
038: * at that index.</p>
039: *
040: * <p>This resolver can be constructed in read-only mode, which means that
041: * {@link #isReadOnly} will always return <code>true</code> and
042: * {@link #setValue} will always throw
043: * <code>PropertyNotWritableException</code>.</p>
044: *
045: * <p><code>ELResolver</code>s are combined together using
046: * {@link CompositeELResolver}s, to define rich semantics for evaluating
047: * an expression. See the javadocs for {@link ELResolver} for details.</p>
048: *
049: * @see CompositeELResolver
050: * @see ELResolver
051: * @see java.util.List
052: * @since JSP 2.1
053: */
054: public class ListELResolver extends ELResolver {
055:
056: /**
057: * Creates a new read/write <code>ListELResolver</code>.
058: */
059: public ListELResolver() {
060: this .isReadOnly = false;
061: }
062:
063: /**
064: * Creates a new <code>ListELResolver</code> whose read-only status is
065: * determined by the given parameter.
066: *
067: * @param isReadOnly <code>true</code> if this resolver cannot modify
068: * lists; <code>false</code> otherwise.
069: */
070: public ListELResolver(boolean isReadOnly) {
071: this .isReadOnly = isReadOnly;
072: }
073:
074: /**
075: * If the base object is a list, returns the most general acceptable type
076: * for a value in this list.
077: *
078: * <p>If the base is a <code>List</code>, the <code>propertyResolved</code>
079: * property of the <code>ELContext</code> object must be set to
080: * <code>true</code> by this resolver, before returning. If this property
081: * is not <code>true</code> after this method is called, the caller
082: * should ignore the return value.</p>
083: *
084: * <p>Assuming the base is a <code>List</code>, this method will always
085: * return <code>Object.class</code>. This is because <code>List</code>s
086: * accept any object as an element.</p>
087: *
088: * @param context The context of this evaluation.
089: * @param base The list to analyze. Only bases of type <code>List</code>
090: * are handled by this resolver.
091: * @param property The index of the element in the list to return the
092: * acceptable type for. Will be coerced into an integer, but
093: * otherwise ignored by this resolver.
094: * @return If the <code>propertyResolved</code> property of
095: * <code>ELContext</code> was set to <code>true</code>, then
096: * the most general acceptable type; otherwise undefined.
097: * @throws PropertyNotFoundException if the given index is out of
098: * bounds for this list.
099: * @throws NullPointerException if context is <code>null</code>
100: * @throws ELException if an exception was thrown while performing
101: * the property or variable resolution. The thrown exception
102: * must be included as the cause property of this exception, if
103: * available.
104: */
105: public Class<?> getType(ELContext context, Object base,
106: Object property) {
107:
108: if (context == null) {
109: throw new NullPointerException();
110: }
111:
112: if (base != null && base instanceof List) {
113: context.setPropertyResolved(true);
114: List list = (List) base;
115: int index = toInteger(property);
116: if (index < 0 || index >= list.size()) {
117: throw new PropertyNotFoundException();
118: }
119: return Object.class;
120: }
121: return null;
122: }
123:
124: /**
125: * If the base object is a list, returns the value at the given index.
126: * The index is specified by the <code>property</code> argument, and
127: * coerced into an integer. If the coercion could not be performed,
128: * an <code>IllegalArgumentException</code> is thrown. If the index is
129: * out of bounds, <code>null</code> is returned.
130: *
131: * <p>If the base is a <code>List</code>, the <code>propertyResolved</code>
132: * property of the <code>ELContext</code> object must be set to
133: * <code>true</code> by this resolver, before returning. If this property
134: * is not <code>true</code> after this method is called, the caller
135: * should ignore the return value.</p>
136: *
137: * @param context The context of this evaluation.
138: * @param base The list to be analyzed. Only bases of type
139: * <code>List</code> are handled by this resolver.
140: * @param property The index of the value to be returned. Will be coerced
141: * into an integer.
142: * @return If the <code>propertyResolved</code> property of
143: * <code>ELContext</code> was set to <code>true</code>, then
144: * the value at the given index or <code>null</code>
145: * if the index was out of bounds. Otherwise, undefined.
146: * @throws IllegalArgumentException if the property could not be coerced
147: * into an integer.
148: * @throws NullPointerException if context is <code>null</code>.
149: * @throws ELException if an exception was thrown while performing
150: * the property or variable resolution. The thrown exception
151: * must be included as the cause property of this exception, if
152: * available.
153: */
154: public Object getValue(ELContext context, Object base,
155: Object property) {
156:
157: if (context == null) {
158: throw new NullPointerException();
159: }
160:
161: if (base != null && base instanceof List) {
162: context.setPropertyResolved(true);
163: List list = (List) base;
164: int index = toInteger(property);
165: if (index < 0 || index >= list.size()) {
166: return null;
167: }
168: return list.get(index);
169: }
170: return null;
171: }
172:
173: /**
174: * If the base object is a list, attempts to set the value at the
175: * given index with the given value. The index is specified by the
176: * <code>property</code> argument, and coerced into an integer. If the
177: * coercion could not be performed, an
178: * <code>IllegalArgumentException</code> is thrown. If the index is
179: * out of bounds, a <code>PropertyNotFoundException</code> is thrown.
180: *
181: * <p>If the base is a <code>List</code>, the <code>propertyResolved</code>
182: * property of the <code>ELContext</code> object must be set to
183: * <code>true</code> by this resolver, before returning. If this property
184: * is not <code>true</code> after this method is called, the caller
185: * can safely assume no value was set.</p>
186: *
187: * <p>If this resolver was constructed in read-only mode, this method will
188: * always throw <code>PropertyNotWritableException</code>.</p>
189: *
190: * <p>If a <code>List</code> was created using
191: * {@link java.util.Collections#unmodifiableList}, this method must
192: * throw <code>PropertyNotWritableException</code>. Unfortunately,
193: * there is no Collections API method to detect this. However, an
194: * implementation can create a prototype unmodifiable <code>List</code>
195: * and query its runtime type to see if it matches the runtime type of
196: * the base object as a workaround.</p>
197: *
198: * @param context The context of this evaluation.
199: * @param base The list to be modified. Only bases of type
200: * <code>List</code> are handled by this resolver.
201: * @param property The index of the value to be set. Will be coerced
202: * into an integer.
203: * @param val The value to be set at the given index.
204: * @throws ClassCastException if the class of the specified element
205: * prevents it from being added to this list.
206: * @throws NullPointerException if context is <code>null</code>, or
207: * if the value is <code>null</code> and this <code>List</code>
208: * does not support <code>null</code> elements.
209: * @throws IllegalArgumentException if the property could not be coerced
210: * into an integer, or if some aspect of the specified element
211: * prevents it from being added to this list.
212: * @throws PropertyNotWritableException if this resolver was constructed
213: * in read-only mode, or if the set operation is not supported by
214: * the underlying list.
215: * @throws PropertyNotFoundException if the given index is out of
216: * bounds for this list.
217: * @throws ELException if an exception was thrown while performing
218: * the property or variable resolution. The thrown exception
219: * must be included as the cause property of this exception, if
220: * available.
221: */
222: public void setValue(ELContext context, Object base,
223: Object property, Object val) {
224:
225: if (context == null) {
226: throw new NullPointerException();
227: }
228:
229: if (base != null && base instanceof List) {
230: context.setPropertyResolved(true);
231: List list = (List) base;
232: int index = toInteger(property);
233: if (isReadOnly) {
234: throw new PropertyNotWritableException();
235: }
236: try {
237: list.set(index, val);
238: } catch (UnsupportedOperationException ex) {
239: throw new PropertyNotWritableException();
240: } catch (IndexOutOfBoundsException ex) {
241: throw new PropertyNotFoundException();
242: } catch (ClassCastException ex) {
243: throw ex;
244: } catch (NullPointerException ex) {
245: throw ex;
246: } catch (IllegalArgumentException ex) {
247: throw ex;
248: }
249: }
250: }
251:
252: static private Class<?> theUnmodifiableListClass = Collections
253: .unmodifiableList(new ArrayList()).getClass();
254:
255: /**
256: * If the base object is a list, returns whether a call to
257: * {@link #setValue} will always fail.
258: *
259: * <p>If the base is a <code>List</code>, the <code>propertyResolved</code>
260: * property of the <code>ELContext</code> object must be set to
261: * <code>true</code> by this resolver, before returning. If this property
262: * is not <code>true</code> after this method is called, the caller
263: * should ignore the return value.</p>
264: *
265: * <p>If this resolver was constructed in read-only mode, this method will
266: * always return <code>true</code>.</p>
267: *
268: * <p>If a <code>List</code> was created using
269: * {@link java.util.Collections#unmodifiableList}, this method must
270: * return <code>true</code>. Unfortunately, there is no Collections API
271: * method to detect this. However, an implementation can create a
272: * prototype unmodifiable <code>List</code> and query its runtime type
273: * to see if it matches the runtime type of the base object as a
274: * workaround.</p>
275: *
276: * @param context The context of this evaluation.
277: * @param base The list to analyze. Only bases of type <code>List</code>
278: * are handled by this resolver.
279: * @param property The index of the element in the list to return the
280: * acceptable type for. Will be coerced into an integer, but
281: * otherwise ignored by this resolver.
282: * @return If the <code>propertyResolved</code> property of
283: * <code>ELContext</code> was set to <code>true</code>, then
284: * <code>true</code> if calling the <code>setValue</code> method
285: * will always fail or <code>false</code> if it is possible that
286: * such a call may succeed; otherwise undefined.
287: * @throws PropertyNotFoundException if the given index is out of
288: * bounds for this list.
289: * @throws NullPointerException if context is <code>null</code>
290: * @throws ELException if an exception was thrown while performing
291: * the property or variable resolution. The thrown exception
292: * must be included as the cause property of this exception, if
293: * available.
294: */
295: public boolean isReadOnly(ELContext context, Object base,
296: Object property) {
297:
298: if (context == null) {
299: throw new NullPointerException();
300: }
301:
302: if (base != null && base instanceof List) {
303: context.setPropertyResolved(true);
304: List list = (List) base;
305: int index = toInteger(property);
306: if (index < 0 || index >= list.size()) {
307: throw new PropertyNotFoundException();
308: }
309: return list.getClass() == theUnmodifiableListClass
310: || isReadOnly;
311: }
312: return false;
313: }
314:
315: /**
316: * Always returns <code>null</code>, since there is no reason to
317: * iterate through set set of all integers.
318: *
319: * <p>The {@link #getCommonPropertyType} method returns sufficient
320: * information about what properties this resolver accepts.</p>
321: *
322: * @param context The context of this evaluation.
323: * @param base The list. Only bases of type <code>List</code> are
324: * handled by this resolver.
325: * @return <code>null</code>.
326: */
327: public Iterator<FeatureDescriptor> getFeatureDescriptors(
328: ELContext context, Object base) {
329: return null;
330: }
331:
332: /**
333: * If the base object is a list, returns the most general type that
334: * this resolver accepts for the <code>property</code> argument.
335: * Otherwise, returns <code>null</code>.
336: *
337: * <p>Assuming the base is a <code>List</code>, this method will always
338: * return <code>Integer.class</code>. This is because <code>List</code>s
339: * accept integers as their index.</p>
340: *
341: * @param context The context of this evaluation.
342: * @param base The list to analyze. Only bases of type <code>List</code>
343: * are handled by this resolver.
344: * @return <code>null</code> if base is not a <code>List</code>; otherwise
345: * <code>Integer.class</code>.
346: */
347: public Class<?> getCommonPropertyType(ELContext context, Object base) {
348: if (base != null && base instanceof List) {
349: return Integer.class;
350: }
351: return null;
352: }
353:
354: private int toInteger(Object p) {
355: if (p instanceof Integer) {
356: return ((Integer) p).intValue();
357: }
358: if (p instanceof Character) {
359: return ((Character) p).charValue();
360: }
361: if (p instanceof Boolean) {
362: return ((Boolean) p).booleanValue() ? 1 : 0;
363: }
364: if (p instanceof Number) {
365: return ((Number) p).intValue();
366: }
367: if (p instanceof String) {
368: return Integer.parseInt((String) p);
369: }
370: throw new IllegalArgumentException();
371: }
372:
373: private boolean isReadOnly;
374: }
|