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