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;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import org.apache.commons.jexl.parser.SimpleNode;
023:
024: /**
025: * Instances of ExpressionImpl are created by the {@link ExpressionFactory},
026: * and this is the default implementation of the {@link Expression} interface.
027: *
028: * @since 1.0
029: * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
030: * @version $Id: ExpressionImpl.java 397111 2006-04-26 06:47:52Z dion $
031: */
032: class ExpressionImpl implements Expression {
033:
034: /** resolvers called before expression evaluation. */
035: protected List preResolvers;
036:
037: /** resolvers called after expression evaluation. */
038: protected List postResolvers;
039:
040: /**
041: * Original expression. This is just a 'snippet', not a valid statement
042: * (i.e. foo.bar() vs foo.bar();
043: */
044: protected String expression;
045:
046: /**
047: * The resulting AST we can call value() on.
048: */
049: protected SimpleNode node;
050:
051: /**
052: * do not let this be generally instantiated with a 'new'.
053: *
054: * @param expr the expression.
055: * @param ref the parsed expression.
056: */
057: ExpressionImpl(String expr, SimpleNode ref) {
058: expression = expr;
059: node = ref;
060: }
061:
062: /**
063: * {@inheritDoc}
064: */
065: public Object evaluate(JexlContext context) throws Exception {
066: Object val = null;
067:
068: /*
069: * if we have pre resolvers, give them a wack
070: */
071: if (preResolvers != null) {
072: val = tryResolver(preResolvers, context);
073:
074: if (val != JexlExprResolver.NO_VALUE) {
075: return val;
076: }
077: }
078:
079: val = node.value(context);
080:
081: /*
082: * if null, call post resolvers
083: */
084: if (val == null && postResolvers != null) {
085: val = tryResolver(postResolvers, context);
086:
087: if (val != JexlExprResolver.NO_VALUE) {
088: return val;
089: }
090: }
091:
092: return val;
093: }
094:
095: /**
096: * Tries the resolvers in the given resolverlist against the context.
097: *
098: * @param resolverList list of JexlExprResolvers
099: * @param context JexlContext to use for evauluation
100: * @return value (including null) or JexlExprResolver.NO_VALUE
101: */
102: protected Object tryResolver(List resolverList, JexlContext context) {
103: Object val = JexlExprResolver.NO_VALUE;
104: String expr = getExpression();
105:
106: for (int i = 0; i < resolverList.size(); i++) {
107: JexlExprResolver jer = (JexlExprResolver) resolverList
108: .get(i);
109:
110: val = jer.evaluate(context, expr);
111:
112: /*
113: * as long as it's not NO_VALUE, return it
114: */
115: if (val != JexlExprResolver.NO_VALUE) {
116: return val;
117: }
118: }
119:
120: return val;
121: }
122:
123: /**
124: * {@inheritDoc}
125: */
126: public String getExpression() {
127: return expression;
128: }
129:
130: /**
131: * {@inheritDoc}
132: */
133: public void addPreResolver(JexlExprResolver resolver) {
134: if (preResolvers == null) {
135: preResolvers = new ArrayList();
136: }
137: preResolvers.add(resolver);
138: }
139:
140: /**
141: * {@inheritDoc}
142: */
143: public void addPostResolver(JexlExprResolver resolver) {
144: if (postResolvers == null) {
145: postResolvers = new ArrayList();
146: }
147: postResolvers.add(resolver);
148: }
149:
150: /**
151: * Provide a string representation of the expression.
152: *
153: * @return the expression or blank if it's null.
154: */
155: public String toString() {
156: String expr = getExpression();
157: return expr == null ? "" : expr;
158: }
159:
160: }
|