KickJava   Java API By Example, From Geeks To Geeks.

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


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 WithExpression extends CompositeExpression {
25
26     private String JavaDoc fVariable;
27     private static final String JavaDoc ATT_VARIABLE= "variable"; //$NON-NLS-1$
28

29     /**
30      * The seed for the hash code for all with expressions.
31      */

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