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 * https://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.jexl3;
18
19 import java.util.Collections;
20 import java.util.Map;
21
22 import org.apache.commons.jexl3.annotations.NoJexl;
23
24 /**
25 * A JEXL evaluation environment wrapping variables, namespace and options.
26 */
27 public class JexlEvalContext implements
28 JexlContext,
29 JexlContext.NamespaceResolver,
30 JexlContext.OptionsHandle {
31 /** The marker for the empty vars. */
32 private static final Map<String,Object> EMPTY_MAP = Collections.<String,Object>emptyMap();
33 /** The variables.*/
34 private final JexlContext vars;
35 /** The namespace. */
36 private final JexlContext.NamespaceResolver ns;
37 /** The options. */
38 private final JexlOptions options = new JexlOptions();
39
40 /**
41 * Default constructor.
42 */
43 @NoJexl
44 public JexlEvalContext() {
45 this(EMPTY_MAP);
46 }
47
48 /**
49 * Creates an evaluation environment from a context.
50 * @param context the context (may be null, implies readonly)
51 */
52 @NoJexl
53 public JexlEvalContext(final JexlContext context) {
54 this(context, context instanceof JexlContext.NamespaceResolver? (JexlContext.NamespaceResolver) context : null);
55 }
56
57 /**
58 * Creates an evaluation environment from a context and a namespace.
59 * @param context the context (may be null, implies readonly)
60 * @param namespace the namespace (may be null, implies empty namespace)
61 */
62 @NoJexl
63 public JexlEvalContext(final JexlContext context, final JexlContext.NamespaceResolver namespace) {
64 this.vars = context != null ? context : JexlEngine.EMPTY_CONTEXT;
65 this.ns = namespace != null ? namespace : JexlEngine.EMPTY_NS;
66 }
67
68 /**
69 * Creates an evaluation environment wrapping an existing user provided vars.
70 * <p>The supplied vars should be null only in derived classes that override the get/set/has methods.
71 * For a default vars context with a code supplied vars, use the default no-parameter contructor.</p>
72 * @param map the variables map
73 */
74 @NoJexl
75 public JexlEvalContext(final Map<String, Object> map) {
76 this.vars = map == EMPTY_MAP ? new MapContext() : new MapContext(map);
77 this.ns = null;
78 }
79
80 @Override
81 @NoJexl
82 public Object get(final String name) {
83 return vars.get(name);
84 }
85
86 @Override
87 @NoJexl
88 public JexlOptions getEngineOptions() {
89 return options;
90 }
91
92 @Override
93 @NoJexl
94 public boolean has(final String name) {
95 return vars.has(name);
96 }
97
98 @Override
99 @NoJexl
100 public Object resolveNamespace(final String name) {
101 return ns != null ? ns.resolveNamespace(name) : null;
102 }
103
104 @Override
105 @NoJexl
106 public void set(final String name, final Object value) {
107 vars.set(name, value);
108 }
109
110 }