KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > expressions > ResolveExpression


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.internal.expressions;
12
13 import org.w3c.dom.Element JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IConfigurationElement;
18
19 import org.eclipse.core.expressions.EvaluationContext;
20 import org.eclipse.core.expressions.EvaluationResult;
21 import org.eclipse.core.expressions.ExpressionInfo;
22 import org.eclipse.core.expressions.IEvaluationContext;
23
24 public class ResolveExpression extends CompositeExpression {
25
26     private String JavaDoc fVariable;
27     private Object JavaDoc[] fArgs;
28     
29     private static final String JavaDoc ATT_VARIABLE= "variable"; //$NON-NLS-1$
30
private static final String JavaDoc ATT_ARGS= "args"; //$NON-NLS-1$
31

32     /**
33      * The seed for the hash code for all resolve expressions.
34      */

35     private static final int HASH_INITIAL= ResolveExpression.class.getName().hashCode();
36     
37     public ResolveExpression(IConfigurationElement configElement) throws CoreException {
38         fVariable= configElement.getAttribute(ATT_VARIABLE);
39         Expressions.checkAttribute(ATT_VARIABLE, fVariable);
40         fArgs= Expressions.getArguments(configElement, ATT_ARGS);
41     }
42
43     public ResolveExpression(Element JavaDoc element) throws CoreException {
44         fVariable= element.getAttribute(ATT_VARIABLE);
45         Expressions.checkAttribute(ATT_VARIABLE, fVariable.length() > 0 ? fVariable : null);
46         fArgs= Expressions.getArguments(element, ATT_ARGS);
47     }
48
49     public ResolveExpression(String JavaDoc variable, Object JavaDoc[] args) {
50         Assert.isNotNull(variable);
51         fVariable= variable;
52         fArgs= args;
53     }
54
55     public EvaluationResult evaluate(IEvaluationContext context) throws CoreException {
56         Object JavaDoc variable= context.resolveVariable(fVariable, fArgs);
57         if (variable == null) {
58             throw new CoreException(new ExpressionStatus(
59                 ExpressionStatus.VARIABLE_NOT_DEFINED,
60                 Messages.format(ExpressionMessages.ResolveExpression_variable_not_defined, fVariable)));
61         }
62         return evaluateAnd(new EvaluationContext(context, variable));
63     }
64     
65     public void collectExpressionInfo(ExpressionInfo info) {
66         ExpressionInfo other= new ExpressionInfo();
67         super.collectExpressionInfo(other);
68         if (other.hasDefaultVariableAccess()) {
69             info.addVariableNameAccess(fVariable);
70         }
71         info.mergeExceptDefaultVariable(other);
72     }
73
74     public boolean equals(final Object JavaDoc object) {
75         if (!(object instanceof ResolveExpression))
76             return false;
77         
78         final ResolveExpression that= (ResolveExpression)object;
79         return this.fVariable.equals(that.fVariable)
80                 && equals(this.fArgs, that.fArgs)
81                 && equals(this.fExpressions, that.fExpressions);
82     }
83
84     protected int computeHashCode() {
85         return HASH_INITIAL * HASH_FACTOR + hashCode(fExpressions)
86             * HASH_FACTOR + hashCode(fArgs)
87             * HASH_FACTOR + fVariable.hashCode();
88     }
89 }
90
Popular Tags