1 package net.sf.saxon.expr; 2 import net.sf.saxon.om.NamePool; 3 import net.sf.saxon.om.ValueRepresentation; 4 import net.sf.saxon.trans.XPathException; 5 import net.sf.saxon.value.EmptySequence; 6 7 import java.util.ArrayList ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 11 15 16 public abstract class Assignation extends ComputedExpression implements Binding { 17 18 protected int slotNumber = -999; protected Expression sequence; protected Expression action; protected String variableName; 23 protected int nameCode; 24 25 protected transient RangeVariableDeclaration declaration; 26 27 30 31 public void setVariableDeclaration (RangeVariableDeclaration decl) { 32 declaration = decl; 33 nameCode = decl.getNameCode(); 34 variableName = decl.getVariableName(); 35 } 36 37 40 41 public RangeVariableDeclaration getVariableDeclaration() { 42 return declaration; 43 } 44 45 54 55 public void setAction(Expression action) { 56 this.action = action; 57 if (declaration != null) { 58 declaration.fixupReferences(this); 59 } 60 adoptChildExpression(action); 61 } 62 63 67 68 public final boolean isGlobal() { 69 return false; 70 } 71 72 77 78 public final boolean isAssignable() { 79 return false; 80 } 81 82 86 87 public int getLocalSlotNumber() { 88 return slotNumber; 89 } 90 91 94 95 public Expression getAction() { 96 return action; 97 } 98 99 102 103 public void setSequence(Expression sequence) { 104 this.sequence = sequence; 105 adoptChildExpression(sequence); 106 } 107 108 111 112 public void setSlotNumber(int nr) { 113 slotNumber = nr; 114 } 115 116 119 120 public int getRequiredSlots() { 121 return 1; 122 } 123 124 127 128 public Expression simplify(StaticContext env) throws XPathException { 129 sequence = sequence.simplify(env); 130 action = action.simplify(env); 131 return this; 132 } 133 134 135 138 139 public Expression promote(PromotionOffer offer) throws XPathException { 140 Expression exp = offer.accept(this); 141 if (exp != null) { 142 return exp; 143 } else { 144 sequence = doPromotion(sequence, offer); 145 if (offer.action == PromotionOffer.INLINE_VARIABLE_REFERENCES || 146 offer.action == PromotionOffer.UNORDERED || 147 offer.action == PromotionOffer.REPLACE_CURRENT) { 148 action = doPromotion(action, offer); 149 } else if (offer.action == PromotionOffer.RANGE_INDEPENDENT) { 150 Binding[] savedBindingList = offer.bindingList; 153 Binding[] newBindingList = extendBindingList(offer.bindingList); 154 offer.bindingList = newBindingList; 155 action = doPromotion(action, offer); 156 offer.bindingList = savedBindingList; 157 } 158 return this; 159 } 160 } 161 162 165 166 protected Binding[] extendBindingList(Binding[] in) { 167 Binding[] newBindingList = new Binding[in.length+1]; 168 System.arraycopy(in, 0, newBindingList, 0, in.length); 169 newBindingList[in.length] = this; 170 return newBindingList; 171 } 172 173 183 184 protected Expression promoteWhereClause(Binding positionBinding) { 185 if (action instanceof IfExpression) { 186 Container container = getParentExpression(); 187 IfExpression ifex = (IfExpression)action; 188 Expression condition = ifex.getCondition(); 189 Expression elseex = ifex.getElseExpression(); 190 if (elseex instanceof EmptySequence) { 191 Binding[] bindingList; 192 if (positionBinding == null) { 193 Binding[] bl = {this}; 194 bindingList = bl; 195 } else { 196 Binding[] bl = {this, positionBinding}; 197 bindingList = bl; 198 } 199 List list = new ArrayList (5); 200 Expression promotedCondition = null; 201 BooleanExpression.listAndComponents(condition, list); 202 for (int i=list.size()-1; i>=0; i--) { 203 Expression term = (Expression)list.get(i); 204 if (!ExpressionTool.dependsOnVariable(term, bindingList)) { 205 if (promotedCondition == null) { 206 promotedCondition = term; 207 } else { 208 promotedCondition = new BooleanExpression(term, Token.AND, promotedCondition); 209 } 210 list.remove(i); 211 } 212 } 213 if (promotedCondition != null) { 214 if (list.size() == 0) { 215 Expression oldThen = ifex.getThenExpression(); 217 setAction(oldThen); 218 ifex.setParentExpression(container); 219 ifex.setThenExpression(this); 220 return ifex; 221 } else { 222 Expression retainedCondition = (Expression)list.get(0); 224 for (int i=1; i<list.size(); i++) { 225 retainedCondition = new BooleanExpression( 226 retainedCondition, Token.AND, (Expression)list.get(i)); 227 } 228 ifex.setCondition(retainedCondition); 229 IfExpression newIf = new IfExpression( 230 promotedCondition, this, EmptySequence.getInstance()); 231 newIf.setParentExpression(container); 232 return newIf; 233 } 234 } 235 236 } 237 } 238 return null; 239 } 240 241 244 245 public Iterator iterateSubExpressions() { 246 return new PairIterator(sequence, action); 247 } 248 249 252 public int getVariableNameCode() { 253 return nameCode; 254 } 255 256 public int getVariableFingerprint() { 257 return nameCode & 0xfffff; 258 } 259 260 263 264 public String getVariableName(NamePool pool) { 265 if (variableName == null) { 266 return "zz:var" + hashCode(); 267 } else { 268 return variableName; 269 } 270 } 271 272 275 276 public ValueRepresentation evaluateVariable(XPathContext context) throws XPathException { 277 return context.evaluateLocalVariable(slotNumber); 278 } 279 280 } 281 282 283 284 | Popular Tags |