KickJava   Java API By Example, From Geeks To Geeks.

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


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.EvaluationResult;
20 import org.eclipse.core.expressions.Expression;
21 import org.eclipse.core.expressions.ExpressionInfo;
22 import org.eclipse.core.expressions.IEvaluationContext;
23
24 public class TestExpression extends Expression {
25
26     private String JavaDoc fNamespace;
27     private String JavaDoc fProperty;
28     private Object JavaDoc[] fArgs;
29     private Object JavaDoc fExpectedValue;
30     private boolean fForcePluginActivation;
31     
32     private static final String JavaDoc ATT_PROPERTY= "property"; //$NON-NLS-1$
33
private static final String JavaDoc ATT_ARGS= "args"; //$NON-NLS-1$
34
private static final String JavaDoc ATT_FORCE_PLUGIN_ACTIVATION= "forcePluginActivation"; //$NON-NLS-1$
35
/**
36      * The seed for the hash code for all test expressions.
37      */

38     private static final int HASH_INITIAL= TestExpression.class.getName().hashCode();
39     
40     private static final TypeExtensionManager fgTypeExtensionManager= new TypeExtensionManager("propertyTesters"); //$NON-NLS-1$
41

42     public TestExpression(IConfigurationElement element) throws CoreException {
43         String JavaDoc property= element.getAttribute(ATT_PROPERTY);
44         int pos= property.lastIndexOf('.');
45         if (pos == -1) {
46             throw new CoreException(new ExpressionStatus(
47                 ExpressionStatus.NO_NAMESPACE_PROVIDED,
48                 ExpressionMessages.TestExpression_no_name_space));
49         }
50         fNamespace= property.substring(0, pos);
51         fProperty= property.substring(pos + 1);
52         fArgs= Expressions.getArguments(element, ATT_ARGS);
53         fExpectedValue= Expressions.convertArgument(element.getAttribute(ATT_VALUE));
54         fForcePluginActivation= Expressions.getOptionalBooleanAttribute(element, ATT_FORCE_PLUGIN_ACTIVATION);
55     }
56
57     public TestExpression(Element JavaDoc element) throws CoreException {
58         String JavaDoc property= element.getAttribute(ATT_PROPERTY);
59         int pos= property.lastIndexOf('.');
60         if (pos == -1) {
61             throw new CoreException(new ExpressionStatus(
62                 ExpressionStatus.NO_NAMESPACE_PROVIDED,
63                 ExpressionMessages.TestExpression_no_name_space));
64         }
65         fNamespace= property.substring(0, pos);
66         fProperty= property.substring(pos + 1);
67         fArgs= Expressions.getArguments(element, ATT_ARGS);
68         String JavaDoc value = element.getAttribute(ATT_VALUE);
69         fExpectedValue= Expressions.convertArgument(value.length() > 0 ? value : null);
70         fForcePluginActivation= Expressions.getOptionalBooleanAttribute(element, ATT_FORCE_PLUGIN_ACTIVATION);
71     }
72
73     public TestExpression(String JavaDoc namespace, String JavaDoc property, Object JavaDoc[] args, Object JavaDoc expectedValue) {
74         this(namespace, property, args, expectedValue, false);
75     }
76     
77     public TestExpression(String JavaDoc namespace, String JavaDoc property, Object JavaDoc[] args, Object JavaDoc expectedValue, boolean forcePluginActivation) {
78         Assert.isNotNull(namespace);
79         Assert.isNotNull(property);
80         fNamespace= namespace;
81         fProperty= property;
82         fArgs= args != null ? args : Expressions.EMPTY_ARGS;
83         fExpectedValue= expectedValue;
84         fForcePluginActivation= forcePluginActivation;
85     }
86
87     public EvaluationResult evaluate(IEvaluationContext context) throws CoreException {
88         Object JavaDoc element= context.getDefaultVariable();
89         if (System JavaDoc.class.equals(element)) {
90             String JavaDoc str= System.getProperty(fProperty);
91             if (str == null)
92                 return EvaluationResult.FALSE;
93             return EvaluationResult.valueOf(str.equals(fArgs[0]));
94         }
95         Property property= fgTypeExtensionManager.getProperty(element, fNamespace, fProperty, context.getAllowPluginActivation() && fForcePluginActivation);
96         if (!property.isInstantiated())
97             return EvaluationResult.NOT_LOADED;
98         return EvaluationResult.valueOf(property.test(element, fArgs, fExpectedValue));
99     }
100
101     public void collectExpressionInfo(ExpressionInfo info) {
102         info.markDefaultVariableAccessed();
103     }
104
105     public boolean equals(final Object JavaDoc object) {
106         if (!(object instanceof TestExpression))
107             return false;
108         
109         final TestExpression that= (TestExpression)object;
110         return this.fNamespace.equals(that.fNamespace) && this.fProperty.equals(that.fProperty)
111             && this.fForcePluginActivation == that.fForcePluginActivation
112             && equals(this.fArgs, that.fArgs) && equals(this.fExpectedValue, that.fExpectedValue);
113     }
114
115     protected int computeHashCode() {
116         return HASH_INITIAL * HASH_FACTOR + hashCode(fArgs)
117             * HASH_FACTOR + hashCode(fExpectedValue)
118             * HASH_FACTOR + fNamespace.hashCode()
119             * HASH_FACTOR + fProperty.hashCode()
120             * HASH_FACTOR + (fForcePluginActivation ? 1 : 0);
121     }
122     
123     //---- Debugging ---------------------------------------------------
124

125     /* (non-Javadoc)
126      * @see java.lang.Object#toString()
127      */

128     public String JavaDoc toString() {
129         StringBuffer JavaDoc args= new StringBuffer JavaDoc();
130         for (int i= 0; i < fArgs.length; i++) {
131             Object JavaDoc arg= fArgs[i];
132             if (arg instanceof String JavaDoc) {
133                 args.append('\'');
134                 args.append(arg);
135                 args.append('\'');
136             } else {
137                 args.append(arg.toString());
138             }
139             if (i < fArgs.length - 1)
140                 args.append(", "); //$NON-NLS-1$
141
}
142         return "<test property=\"" + fProperty + //$NON-NLS-1$
143
(fArgs.length != 0 ? "\" args=\"" + args + "\"" : "\"") + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
144
(fExpectedValue != null ? "\" value=\"" + fExpectedValue + "\"" : "\"") + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
145
" plug-in activation: " + (fForcePluginActivation ? "eager" : "lazy") + //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
146
"/>"; //$NON-NLS-1$
147
}
148     
149     //---- testing ---------------------------------------------------
150

151     public boolean testGetForcePluginActivation() {
152         return fForcePluginActivation;
153     }
154     
155     public static TypeExtensionManager testGetTypeExtensionManager() {
156         return fgTypeExtensionManager;
157     }
158 }
159
Popular Tags