1 11 package org.eclipse.core.internal.expressions; 12 13 import java.util.Collection ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.w3c.dom.Element ; 18 19 import org.eclipse.core.runtime.Assert; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IConfigurationElement; 22 23 import org.eclipse.core.expressions.EvaluationResult; 24 import org.eclipse.core.expressions.ExpressionInfo; 25 import org.eclipse.core.expressions.IEvaluationContext; 26 import org.eclipse.core.expressions.IIterable; 27 28 public class IterateExpression extends CompositeExpression { 29 30 private static class IteratePool implements IEvaluationContext { 31 32 private Iterator fIterator; 33 private Object fDefaultVariable; 34 private IEvaluationContext fParent; 35 36 public IteratePool(IEvaluationContext parent, Iterator iterator) { 37 Assert.isNotNull(parent); 38 Assert.isNotNull(iterator); 39 fParent= parent; 40 fIterator= iterator; 41 } 42 public IEvaluationContext getParent() { 43 return fParent; 44 } 45 public IEvaluationContext getRoot() { 46 return fParent.getRoot(); 47 } 48 public Object getDefaultVariable() { 49 return fDefaultVariable; 50 } 51 public boolean getAllowPluginActivation() { 52 return fParent.getAllowPluginActivation(); 53 } 54 public void setAllowPluginActivation(boolean value) { 55 fParent.setAllowPluginActivation(value); 56 } 57 public void addVariable(String name, Object value) { 58 fParent.addVariable(name, value); 59 } 60 public Object removeVariable(String name) { 61 return fParent.removeVariable(name); 62 } 63 public Object getVariable(String name) { 64 return fParent.getVariable(name); 65 } 66 public Object resolveVariable(String name, Object [] args) throws CoreException { 67 return fParent.resolveVariable(name, args); 68 } 69 public Object next() { 70 fDefaultVariable= fIterator.next(); 71 return fDefaultVariable; 72 } 73 public boolean hasNext() { 74 return fIterator.hasNext(); 75 } 76 } 77 78 private static final String ATT_OPERATOR= "operator"; private static final String ATT_IF_EMPTY= "ifEmpty"; private static final int OR= 1; 81 private static final int AND= 2; 82 83 86 private static final int HASH_INITIAL= IterateExpression.class.getName().hashCode(); 87 88 private int fOperator; 89 private Boolean fEmptyResult; 90 91 public IterateExpression(IConfigurationElement configElement) throws CoreException { 92 String opValue= configElement.getAttribute(ATT_OPERATOR); 93 initializeOperatorValue(opValue); 94 initializeEmptyResultValue(configElement.getAttribute(ATT_IF_EMPTY)); 95 } 96 97 public IterateExpression(Element element) throws CoreException { 98 String opValue= element.getAttribute(ATT_OPERATOR); 99 initializeOperatorValue(opValue.length() > 0 ? opValue : null); 100 String ifEmpty= element.getAttribute(ATT_IF_EMPTY); 101 initializeEmptyResultValue(ifEmpty.length() > 0 ? ifEmpty : null); 102 } 103 104 public IterateExpression(String opValue) throws CoreException { 105 initializeOperatorValue(opValue); 106 } 107 108 public IterateExpression(String opValue, String ifEmpty) throws CoreException { 109 initializeOperatorValue(opValue); 110 initializeEmptyResultValue(ifEmpty); 111 } 112 113 private void initializeOperatorValue(String opValue) throws CoreException { 114 if (opValue == null) { 115 fOperator= AND; 116 } else { 117 Expressions.checkAttribute(ATT_OPERATOR, opValue, new String [] {"and", "or"}); if ("and".equals(opValue)) { fOperator= AND; 120 } else { 121 fOperator= OR; 122 } 123 } 124 } 125 126 private void initializeEmptyResultValue(String value) { 127 if (value == null) { 128 fEmptyResult= null; 129 } else { 130 fEmptyResult= Boolean.valueOf(value); 131 } 132 } 133 134 137 public EvaluationResult evaluate(IEvaluationContext context) throws CoreException { 138 Object var= context.getDefaultVariable(); 139 if (var instanceof Collection ) { 140 Collection col= (Collection )var; 141 switch (col.size()) { 142 case 0: 143 if (fEmptyResult == null) { 144 return fOperator == AND ? EvaluationResult.TRUE : EvaluationResult.FALSE; 145 } else { 146 return fEmptyResult.booleanValue() ? EvaluationResult.TRUE : EvaluationResult.FALSE; 147 } 148 case 1: 149 if (col instanceof List ) 150 return evaluateAnd(new DefaultVariable(context, ((List )col).get(0))); 151 default: 153 IteratePool iter= new IteratePool(context, col.iterator()); 154 EvaluationResult result= fOperator == AND ? EvaluationResult.TRUE : EvaluationResult.FALSE; 155 while (iter.hasNext()) { 156 iter.next(); 157 switch(fOperator) { 158 case OR: 159 result= result.or(evaluateAnd(iter)); 160 if (result == EvaluationResult.TRUE) 161 return result; 162 break; 163 case AND: 164 result= result.and(evaluateAnd(iter)); 165 if (result != EvaluationResult.TRUE) 166 return result; 167 break; 168 } 169 } 170 return result; 171 } 172 } else { 173 IIterable iterable= Expressions.getAsIIterable(var, this); 174 if (iterable == null) 175 return EvaluationResult.NOT_LOADED; 176 int count= 0; 177 IteratePool iter= new IteratePool(context, iterable.iterator()); 178 EvaluationResult result= fOperator == AND ? EvaluationResult.TRUE : EvaluationResult.FALSE; 179 while (iter.hasNext()) { 180 iter.next(); 181 count++; 182 switch(fOperator) { 183 case OR: 184 result= result.or(evaluateAnd(iter)); 185 if (result == EvaluationResult.TRUE) 186 return result; 187 break; 188 case AND: 189 result= result.and(evaluateAnd(iter)); 190 if (result != EvaluationResult.TRUE) 191 return result; 192 break; 193 } 194 } 195 if (count > 0) { 196 return result; 197 } else { 198 if (fEmptyResult == null) { 199 return fOperator == AND ? EvaluationResult.TRUE : EvaluationResult.FALSE; 200 } else { 201 return fEmptyResult.booleanValue() ? EvaluationResult.TRUE : EvaluationResult.FALSE; 202 } 203 } 204 } 205 } 206 207 public void collectExpressionInfo(ExpressionInfo info) { 208 info.markDefaultVariableAccessed(); 212 super.collectExpressionInfo(info); 213 } 214 215 public boolean equals(final Object object) { 216 if (!(object instanceof IterateExpression)) 217 return false; 218 219 final IterateExpression that= (IterateExpression)object; 220 return (this.fOperator == that.fOperator) && equals(this.fExpressions, that.fExpressions); 221 } 222 223 protected int computeHashCode() { 224 return HASH_INITIAL * HASH_FACTOR + hashCode(fExpressions) 225 * HASH_FACTOR + fOperator; 226 } 227 } 228 | Popular Tags |