KickJava   Java API By Example, From Geeks To Geeks.

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


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 SystemTestExpression extends Expression {
25
26     private String JavaDoc fProperty;
27     private String JavaDoc fExpectedValue;
28     
29     private static final String JavaDoc ATT_PROPERTY= "property"; //$NON-NLS-1$
30

31     /**
32      * The seed for the hash code for all system test expressions.
33      */

34     private static final int HASH_INITIAL= SystemTestExpression.class.getName().hashCode();
35     
36     public SystemTestExpression(IConfigurationElement element) throws CoreException {
37         fProperty= element.getAttribute(ATT_PROPERTY);
38         Expressions.checkAttribute(ATT_PROPERTY, fProperty);
39         fExpectedValue= element.getAttribute(ATT_VALUE);
40         Expressions.checkAttribute(ATT_VALUE, fExpectedValue);
41     }
42
43     public SystemTestExpression(Element JavaDoc element) throws CoreException {
44         fProperty= element.getAttribute(ATT_PROPERTY);
45         Expressions.checkAttribute(ATT_PROPERTY, fProperty.length() > 0 ? fProperty : null);
46         fExpectedValue= element.getAttribute(ATT_VALUE);
47         Expressions.checkAttribute(ATT_VALUE, fExpectedValue.length() > 0 ? fExpectedValue : null);
48     }
49
50     public SystemTestExpression(String JavaDoc property, String JavaDoc expectedValue) {
51         Assert.isNotNull(property);
52         Assert.isNotNull(expectedValue);
53         fProperty= property;
54         fExpectedValue= expectedValue;
55     }
56     
57     public EvaluationResult evaluate(IEvaluationContext context) throws CoreException {
58         String JavaDoc str= System.getProperty(fProperty);
59         if (str == null)
60             return EvaluationResult.FALSE;
61         return EvaluationResult.valueOf(str.equals(fExpectedValue));
62     }
63
64     public void collectExpressionInfo(ExpressionInfo info) {
65         info.markSystemPropertyAccessed();
66     }
67
68     public boolean equals(final Object JavaDoc object) {
69         if (!(object instanceof SystemTestExpression))
70             return false;
71         
72         final SystemTestExpression that= (SystemTestExpression)object;
73         return this.fProperty.equals(that.fProperty)
74                 && this.fExpectedValue.equals(that.fExpectedValue);
75     }
76
77     protected int computeHashCode() {
78         return HASH_INITIAL * HASH_FACTOR + fExpectedValue.hashCode()
79             * HASH_FACTOR + fProperty.hashCode();
80     }
81     
82     // ---- Debugging ---------------------------------------------------
83

84     /* (non-Javadoc)
85      * @see java.lang.Object#toString()
86      */

87     public String JavaDoc toString() {
88         return "<systemTest property=\"" + fProperty + //$NON-NLS-1$
89
"\" value=\"" + fExpectedValue + "\""; //$NON-NLS-1$ //$NON-NLS-2$
90
}
91 }
92
Popular Tags