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