001: //--------------------------------------------------------------------------
002: // Copyright (c) 1998-2004, Drew Davidson and Luke Blanshard
003: // All rights reserved.
004: //
005: // Redistribution and use in source and binary forms, with or without
006: // modification, are permitted provided that the following conditions are
007: // met:
008: //
009: // Redistributions of source code must retain the above copyright notice,
010: // this list of conditions and the following disclaimer.
011: // Redistributions in binary form must reproduce the above copyright
012: // notice, this list of conditions and the following disclaimer in the
013: // documentation and/or other materials provided with the distribution.
014: // Neither the name of the Drew Davidson nor the names of its contributors
015: // may be used to endorse or promote products derived from this software
016: // without specific prior written permission.
017: //
018: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
019: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
020: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
021: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
022: // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
023: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
024: // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
025: // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
027: // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
028: // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
029: // DAMAGE.
030: //--------------------------------------------------------------------------
031: package ognl;
032:
033: import java.util.*;
034:
035: /**
036: * Implementation of PropertyAccessor that uses numbers and dynamic subscripts as
037: * properties to index into Lists.
038: * @author Luke Blanshard (blanshlu@netscape.net)
039: * @author Drew Davidson (drew@ognl.org)
040: */
041: public class ListPropertyAccessor extends ObjectPropertyAccessor
042: implements PropertyAccessor // This is here to make javadoc show this class as an implementor
043: {
044: public Object getProperty(Map context, Object target, Object name)
045: throws OgnlException {
046: List list = (List) target;
047:
048: if (name instanceof String) {
049: Object result;
050:
051: if (name.equals("size")) {
052: result = new Integer(list.size());
053: } else {
054: if (name.equals("iterator")) {
055: result = list.iterator();
056: } else {
057: if (name.equals("isEmpty")) {
058: result = list.isEmpty() ? Boolean.TRUE
059: : Boolean.FALSE;
060: } else {
061: result = super .getProperty(context, target,
062: name);
063: }
064: }
065: }
066: return result;
067: }
068:
069: if (name instanceof Number)
070: return list.get(((Number) name).intValue());
071:
072: if (name instanceof DynamicSubscript) {
073: int len = list.size();
074: switch (((DynamicSubscript) name).getFlag()) {
075: case DynamicSubscript.FIRST:
076: return len > 0 ? list.get(0) : null;
077: case DynamicSubscript.MID:
078: return len > 0 ? list.get(len / 2) : null;
079: case DynamicSubscript.LAST:
080: return len > 0 ? list.get(len - 1) : null;
081: case DynamicSubscript.ALL:
082: return new ArrayList(list);
083: }
084: }
085:
086: throw new NoSuchPropertyException(target, name);
087: }
088:
089: public void setProperty(Map context, Object target, Object name,
090: Object value) throws OgnlException {
091: if (name instanceof String) {
092: super .setProperty(context, target, name, value);
093: return;
094: }
095:
096: List list = (List) target;
097:
098: if (name instanceof Number) {
099: list.set(((Number) name).intValue(), value);
100: return;
101: }
102:
103: if (name instanceof DynamicSubscript) {
104: int len = list.size();
105: switch (((DynamicSubscript) name).getFlag()) {
106: case DynamicSubscript.FIRST:
107: if (len > 0)
108: list.set(0, value);
109: return;
110: case DynamicSubscript.MID:
111: if (len > 0)
112: list.set(len / 2, value);
113: return;
114: case DynamicSubscript.LAST:
115: if (len > 0)
116: list.set(len - 1, value);
117: return;
118: case DynamicSubscript.ALL: {
119: if (!(value instanceof Collection))
120: throw new OgnlException(
121: "Value must be a collection");
122: list.clear();
123: list.addAll((Collection) value);
124: return;
125: }
126: }
127: }
128:
129: throw new NoSuchPropertyException(target, name);
130: }
131: }
|