001: /*
002: * Copyright 2002-2006 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.jexl.parser;
018:
019: import org.apache.commons.jexl.JexlContext;
020: import org.apache.commons.jexl.util.Coercion;
021: import org.apache.commons.jexl.util.Introspector;
022: import org.apache.commons.jexl.util.introspection.Info;
023: import org.apache.commons.jexl.util.introspection.VelPropertyGet;
024:
025: import java.util.List;
026: import java.util.Map;
027: import java.lang.reflect.Array;
028:
029: /**
030: * Like an ASTIdentifier, but with array access allowed.
031: *
032: * $foo[2]
033: *
034: * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
035: * @version $Id: ASTArrayAccess.java 398180 2006-04-29 15:40:35Z dion $
036: */
037: public class ASTArrayAccess extends SimpleNode {
038: /** dummy velocity info. */
039: private static final Info DUMMY = new Info("", 1, 1);
040:
041: /**
042: * Create the node given an id.
043: *
044: * @param id node id.
045: */
046: public ASTArrayAccess(int id) {
047: super (id);
048: }
049:
050: /**
051: * Create a node with the given parser and id.
052: *
053: * @param p a parser.
054: * @param id node id.
055: */
056: public ASTArrayAccess(Parser p, int id) {
057: super (p, id);
058: }
059:
060: /** {@inheritDoc} */
061: public Object jjtAccept(ParserVisitor visitor, Object data) {
062: return visitor.visit(this , data);
063: }
064:
065: /**
066: * evaluate array access upon a base object.
067: *
068: * foo.bar[2]
069: *
070: * makes me rethink the array operator :)
071: * @param jc the {@link JexlContext} to evaluate against.
072: * @param obj not used.
073: * @return the value of the array expression.
074: * @throws Exception on any error
075: */
076: public Object execute(Object obj, JexlContext jc) throws Exception {
077: ASTIdentifier base = (ASTIdentifier) jjtGetChild(0);
078:
079: Object result = base.execute(obj, jc);
080:
081: /*
082: * ignore the first child - it's our identifier
083: */
084: for (int i = 1; i < jjtGetNumChildren(); i++) {
085: Object loc = ((SimpleNode) jjtGetChild(i)).value(jc);
086:
087: if (loc == null) {
088: return null;
089: }
090:
091: result = evaluateExpr(result, loc);
092: }
093:
094: return result;
095: }
096:
097: /** {@inheritDoc} */
098: public Object value(JexlContext jc) throws Exception {
099: /*
100: * get the base ASTIdentifier
101: */
102:
103: ASTIdentifier base = (ASTIdentifier) jjtGetChild(0);
104:
105: Object o = base.value(jc);
106:
107: /*
108: * ignore the first child - it's our identifier
109: */
110: for (int i = 1; i < jjtGetNumChildren(); i++) {
111: Object loc = ((SimpleNode) jjtGetChild(i)).value(jc);
112:
113: if (loc == null) {
114: return null;
115: }
116:
117: o = evaluateExpr(o, loc);
118: }
119:
120: return o;
121: }
122:
123: /**
124: * Evaluate the Array expression 'loc' on the given object, o.
125: * e.g. in 'a[2]', <code>2</code> is 'loc' and <code>a</code> is 'o'.
126: *
127: * If o or loc are null, null is returned.
128: * If o is a Map, o.get(loc) is returned.
129: * If o is a List, o.get(loc) is returned. loc must resolve to an int value.
130: * If o is an Array, o[loc] is returned. loc must resolve to an int value.
131: * Otherwise loc is treated as a bean property of o.
132: *
133: * @param o an object to be accessed using the array operator or '.' operator.
134: * @param loc the index of the object to be returned.
135: * @return the resulting value.
136: * @throws Exception on any error.
137: */
138: public static Object evaluateExpr(Object o, Object loc)
139: throws Exception {
140: /*
141: * following the JSTL EL rules
142: */
143:
144: if (o == null) {
145: return null;
146: }
147:
148: if (loc == null) {
149: return null;
150: }
151:
152: if (o instanceof Map) {
153: if (!((Map) o).containsKey(loc)) {
154: return null;
155: }
156:
157: return ((Map) o).get(loc);
158: } else if (o instanceof List) {
159: int idx = Coercion.coerceInteger(loc).intValue();
160:
161: try {
162: return ((List) o).get(idx);
163: } catch (IndexOutOfBoundsException iobe) {
164: return null;
165: }
166: } else if (o.getClass().isArray()) {
167: int idx = Coercion.coerceInteger(loc).intValue();
168:
169: try {
170: return Array.get(o, idx);
171: } catch (ArrayIndexOutOfBoundsException aiobe) {
172: return null;
173: }
174: } else {
175: /*
176: * "Otherwise (a JavaBean object)..." huh? :)
177: */
178:
179: String s = loc.toString();
180:
181: VelPropertyGet vg = Introspector.getUberspect()
182: .getPropertyGet(o, s, DUMMY);
183:
184: if (vg != null) {
185: return vg.invoke(o);
186: }
187: }
188:
189: throw new Exception(
190: "Unsupported object type for array [] accessor");
191: }
192:
193: /**
194: * Gets the variable name piece of the expression.
195: * @return a String of the identifer.
196: * @see ASTIdentifier#getIdentifierString().
197: */
198: public String getIdentifierString() {
199: return ((ASTIdentifier) jjtGetChild(0)).getIdentifierString();
200: }
201: }
|