Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6ce720566bfd302c0753e4566cf60c9bd01c0a43 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*******************************************************************************
 * Copyright (c) 2007, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.core.internal.expressions;

import org.w3c.dom.Element;

import org.eclipse.core.expressions.EvaluationResult;
import org.eclipse.core.expressions.Expression;
import org.eclipse.core.expressions.ExpressionInfo;
import org.eclipse.core.expressions.IEvaluationContext;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;

/**
 * This class makes use of the <b>org.eclipse.core.expressions.definitions</b>
 * extension point to evaluate the current context against pre-defined
 * expressions. It provides core expression re-use.
 *
 * @since 3.3
 */
public class ReferenceExpression extends Expression {

	// consider making this a more general extension manager
	// for now it's just part of the reference expression
	private static DefinitionRegistry fgDefinitionRegistry= null;

	private static DefinitionRegistry getDefinitionRegistry() {
		if (fgDefinitionRegistry == null) {
			fgDefinitionRegistry= new DefinitionRegistry();
		}
		return fgDefinitionRegistry;
	}

	private static final String ATT_DEFINITION_ID= "definitionId"; //$NON-NLS-1$

	/**
	 * The seed for the hash code for all equals expressions.
	 */
	private static final int HASH_INITIAL= ReferenceExpression.class.getName().hashCode();

	private String fDefinitionId;

	public ReferenceExpression(String definitionId) {
		Assert.isNotNull(definitionId);
		fDefinitionId= definitionId;
	}

	public ReferenceExpression(IConfigurationElement element) throws CoreException {
		fDefinitionId= element.getAttribute(ATT_DEFINITION_ID);
		Expressions.checkAttribute(ATT_DEFINITION_ID, fDefinitionId);
	}

	public ReferenceExpression(Element element) throws CoreException {
		fDefinitionId= element.getAttribute(ATT_DEFINITION_ID);
		Expressions.checkAttribute(ATT_DEFINITION_ID, fDefinitionId.length() > 0 ? fDefinitionId : null);
	}

	@Override
	public EvaluationResult evaluate(IEvaluationContext context) throws CoreException {
		Expression expr= getDefinitionRegistry().getExpression(fDefinitionId);
		return expr.evaluate(context);
	}

	@Override
	public void collectExpressionInfo(ExpressionInfo info) {
		Expression expr;
		try {
			expr= getDefinitionRegistry().getExpression(fDefinitionId);
		} catch (CoreException e) {
			// We didn't find the expression definition. So no
			// expression info can be collected.
			return;
		}
		expr.collectExpressionInfo(info);
	}

	@Override
	public boolean equals(final Object object) {
		if (!(object instanceof ReferenceExpression))
			return false;

		final ReferenceExpression that= (ReferenceExpression)object;
		return this.fDefinitionId.equals(that.fDefinitionId);
	}

	@Override
	protected int computeHashCode() {
		return HASH_INITIAL * HASH_FACTOR + fDefinitionId.hashCode();
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder(getClass().getSimpleName());
		builder.append(" [definitionId="); //$NON-NLS-1$
		builder.append(fDefinitionId);
		builder.append("]"); //$NON-NLS-1$
		return builder.toString();
	}
}

Back to the top