1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.jexl.resolver;
18
19 import org.apache.commons.jexl.JexlExprResolver;
20 import org.apache.commons.jexl.JexlContext;
21
22 /**
23 * Simple resolver to try the expression as-is from the context.
24 *
25 * For example, you could resolve ant-ish properties (foo.bar.woogie)
26 * using this...
27 *
28 * hint, hint...
29 *
30 * @since 1.0
31 * @author <a href="mailto:geirm@adeptra.com">Geir Magnusson Jr.</a>
32 * @version $Id: FlatResolver.java 480412 2006-11-29 05:11:23Z bayard $
33 */
34 public class FlatResolver implements JexlExprResolver {
35 /**
36 * Flag to return NO_VALUE on null from context.
37 * this allows jexl to try to evaluate
38 */
39 protected boolean noValOnNull = true;
40
41 /**
42 * Default CTOR.
43 */
44 public FlatResolver() {
45 }
46
47 /**
48 * CTOR that lets you override the default behavior of
49 * noValOnNull, which is true. (jexl gets a shot after if null)
50 *
51 * @param valOnNull Whether NO_VALUE will be returned instead of null.
52 */
53 public FlatResolver(boolean valOnNull) {
54 noValOnNull = valOnNull;
55 }
56
57 /**
58 * Try to resolve expression as-is.
59 *
60 * @param context The context for resolution.
61 * @param expression The flat expression.
62 * @return The resolved value.
63 */
64 public Object evaluate(JexlContext context, String expression) {
65 Object val = context.getVars().get(expression);
66
67 if (val == null && noValOnNull) {
68 return JexlExprResolver.NO_VALUE;
69 }
70
71 return val;
72 }
73 }