1 11 package org.eclipse.core.internal.expressions; 12 13 import org.w3c.dom.Element ; 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 fNamespace; 27 private String fProperty; 28 private Object [] fArgs; 29 private Object fExpectedValue; 30 private boolean fForcePluginActivation; 31 32 private static final String ATT_PROPERTY= "property"; private static final String ATT_ARGS= "args"; private static final String ATT_FORCE_PLUGIN_ACTIVATION= "forcePluginActivation"; 38 private static final int HASH_INITIAL= TestExpression.class.getName().hashCode(); 39 40 private static final TypeExtensionManager fgTypeExtensionManager= new TypeExtensionManager("propertyTesters"); 42 public TestExpression(IConfigurationElement element) throws CoreException { 43 String 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 element) throws CoreException { 58 String 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 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 namespace, String property, Object [] args, Object expectedValue) { 74 this(namespace, property, args, expectedValue, false); 75 } 76 77 public TestExpression(String namespace, String property, Object [] args, Object 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 element= context.getDefaultVariable(); 89 if (System .class.equals(element)) { 90 String 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 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 125 128 public String toString() { 129 StringBuffer args= new StringBuffer (); 130 for (int i= 0; i < fArgs.length; i++) { 131 Object arg= fArgs[i]; 132 if (arg instanceof String ) { 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(", "); } 142 return "<test property=\"" + fProperty + (fArgs.length != 0 ? "\" args=\"" + args + "\"" : "\"") + (fExpectedValue != null ? "\" value=\"" + fExpectedValue + "\"" : "\"") + " plug-in activation: " + (fForcePluginActivation ? "eager" : "lazy") + "/>"; } 148 149 151 public boolean testGetForcePluginActivation() { 152 return fForcePluginActivation; 153 } 154 155 public static TypeExtensionManager testGetTypeExtensionManager() { 156 return fgTypeExtensionManager; 157 } 158 } 159 | Popular Tags |