KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > expressions > EvaluationContext


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.expressions;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.CoreException;
18
19 /**
20  * A default implementation of an evaluation context.
21  * <p>
22  * Clients may instantiate this default context. The class is
23  * not intended to be subclassed by clients.
24  * </p>
25  *
26  * @since 3.0
27  */

28 public class EvaluationContext implements IEvaluationContext {
29
30     private IEvaluationContext fParent;
31     private Object JavaDoc fDefaultVariable;
32     private Map JavaDoc/*<String, Object>*/ fVariables;
33     private IVariableResolver[] fVariableResolvers;
34     private Boolean JavaDoc fAllowPluginActivation;
35     
36     /**
37      * Create a new evaluation context with the given parent and default
38      * variable.
39      *
40      * @param parent the parent context. Can be <code>null</code>.
41      * @param defaultVariable the default variable
42      */

43     public EvaluationContext(IEvaluationContext parent, Object JavaDoc defaultVariable) {
44         Assert.isNotNull(defaultVariable);
45         fParent= parent;
46         fDefaultVariable= defaultVariable;
47     }
48     
49     /**
50      * Create a new evaluation context with the given parent and default
51      * variable.
52      *
53      * @param parent the parent context. Can be <code>null</code>.
54      * @param defaultVariable the default variable
55      * @param resolvers an array of <code>IVariableResolvers</code> to
56      * resolve additional variables.
57      *
58      * @see #resolveVariable(String, Object[])
59      */

60     public EvaluationContext(IEvaluationContext parent, Object JavaDoc defaultVariable, IVariableResolver[] resolvers) {
61         Assert.isNotNull(defaultVariable);
62         Assert.isNotNull(resolvers);
63         fParent= parent;
64         fDefaultVariable= defaultVariable;
65         fVariableResolvers= resolvers;
66     }
67     
68     /**
69      * {@inheritDoc}
70      */

71     public IEvaluationContext getParent() {
72         return fParent;
73     }
74     
75     /**
76      * {@inheritDoc}
77      */

78     public IEvaluationContext getRoot() {
79         if (fParent == null)
80             return this;
81         return fParent.getRoot();
82     }
83     
84     /**
85      * {@inheritDoc}
86      */

87     public Object JavaDoc getDefaultVariable() {
88         return fDefaultVariable;
89     }
90     
91     /**
92      * {@inheritDoc}
93      */

94     public void setAllowPluginActivation(boolean value) {
95         fAllowPluginActivation= value ? Boolean.TRUE : Boolean.FALSE;
96     }
97     
98     /**
99      * {@inheritDoc}
100      */

101     public boolean getAllowPluginActivation() {
102         if (fAllowPluginActivation == null) {
103             if (fParent != null)
104                 return fParent.getAllowPluginActivation();
105             return false;
106         }
107         return fAllowPluginActivation.booleanValue();
108     }
109     
110     /**
111      * {@inheritDoc}
112      */

113     public void addVariable(String JavaDoc name, Object JavaDoc value) {
114         Assert.isNotNull(name);
115         Assert.isNotNull(value);
116         if (fVariables == null)
117             fVariables= new HashMap JavaDoc();
118         fVariables.put(name, value);
119     }
120     
121     /**
122      * {@inheritDoc}
123      */

124     public Object JavaDoc removeVariable(String JavaDoc name) {
125         Assert.isNotNull(name);
126         if (fVariables == null)
127             return null;
128         return fVariables.remove(name);
129     }
130     
131     /**
132      * {@inheritDoc}
133      */

134     public Object JavaDoc getVariable(String JavaDoc name) {
135         Assert.isNotNull(name);
136         Object JavaDoc result= null;
137         if (fVariables != null) {
138             result= fVariables.get(name);
139         }
140         if (result != null)
141             return result;
142         if (fParent != null)
143             return fParent.getVariable(name);
144         return null;
145     }
146     
147     /**
148      * {@inheritDoc}
149      */

150     public Object JavaDoc resolveVariable(String JavaDoc name, Object JavaDoc[] args) throws CoreException {
151         if (fVariableResolvers != null && fVariableResolvers.length > 0) {
152             for (int i= 0; i < fVariableResolvers.length; i++) {
153                 IVariableResolver resolver= fVariableResolvers[i];
154                 Object JavaDoc variable= resolver.resolve(name, args);
155                 if (variable != null)
156                     return variable;
157             }
158         }
159         if (fParent != null)
160             return fParent.resolveVariable(name, args);
161         return null;
162     }
163 }
164
Popular Tags