KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2007 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.EvaluationResult;
20 import org.eclipse.core.expressions.Expression;
21 import org.eclipse.core.expressions.ExpressionInfo;
22 import org.eclipse.core.expressions.IEvaluationContext;
23
24 /**
25  * This class makes use of the <b>org.eclipse.core.expressions.definitions</b>
26  * extension point to evaluate the current context against pre-defined
27  * expressions. It provides core expression re-use.
28  *
29  * @since 3.3
30  */

31 public class ReferenceExpression extends Expression {
32
33     // consider making this a more general extension manager
34
// for now it's just part of the reference expression
35
private static DefinitionRegistry fgDefinitionRegistry= null;
36
37     private static DefinitionRegistry getDefinitionRegistry() {
38         if (fgDefinitionRegistry == null) {
39             fgDefinitionRegistry= new DefinitionRegistry();
40         }
41         return fgDefinitionRegistry;
42     }
43
44     private static final String JavaDoc ATT_DEFINITION_ID= "definitionId"; //$NON-NLS-1$
45

46     /**
47      * The seed for the hash code for all equals expressions.
48      */

49     private static final int HASH_INITIAL= ReferenceExpression.class.getName().hashCode();
50
51     private String JavaDoc fDefinitionId;
52
53     public ReferenceExpression(String JavaDoc definitionId) {
54         Assert.isNotNull(definitionId);
55         fDefinitionId= definitionId;
56     }
57
58     public ReferenceExpression(IConfigurationElement element) throws CoreException {
59         fDefinitionId= element.getAttribute(ATT_DEFINITION_ID);
60         Expressions.checkAttribute(ATT_DEFINITION_ID, fDefinitionId);
61     }
62
63     public ReferenceExpression(Element JavaDoc element) throws CoreException {
64         fDefinitionId= element.getAttribute(ATT_DEFINITION_ID);
65         Expressions.checkAttribute(ATT_DEFINITION_ID, fDefinitionId.length() > 0 ? fDefinitionId : null);
66     }
67
68     public EvaluationResult evaluate(IEvaluationContext context) throws CoreException {
69         Expression expr= getDefinitionRegistry().getExpression(fDefinitionId);
70         return expr.evaluate(context);
71     }
72
73     public void collectExpressionInfo(ExpressionInfo info) {
74         Expression expr;
75         try {
76             expr= getDefinitionRegistry().getExpression(fDefinitionId);
77         } catch (CoreException e) {
78             // We didn't find the expression definition. So no
79
// expression info can be collected.
80
return;
81         }
82         expr.collectExpressionInfo(info);
83     }
84
85     public boolean equals(final Object JavaDoc object) {
86         if (!(object instanceof ReferenceExpression))
87             return false;
88
89         final ReferenceExpression that= (ReferenceExpression)object;
90         return this.fDefinitionId.equals(that.fDefinitionId);
91     }
92
93     protected int computeHashCode() {
94         return HASH_INITIAL * HASH_FACTOR + fDefinitionId.hashCode();
95     }
96 }
Popular Tags