1 package net.sf.saxon.instruct; 2 import net.sf.saxon.expr.*; 3 import net.sf.saxon.om.*; 4 import net.sf.saxon.pattern.NoNodeTest; 5 import net.sf.saxon.style.StandardNames; 6 import net.sf.saxon.trans.XPathException; 7 import net.sf.saxon.type.ItemType; 8 import net.sf.saxon.value.SequenceType; 9 10 import java.io.PrintStream ; 11 import java.util.Collections ; 12 import java.util.Iterator ; 13 14 18 19 public abstract class GeneralVariable extends Instruction implements Binding { 20 21 22 private static final int ASSIGNABLE = 1; 23 private static final int REQUIRED = 4; 24 private static final int TUNNEL = 8; 25 26 private byte properties = 0; 27 Expression select = null; 28 protected int nameCode = -1; 29 SequenceType requiredType; 30 private int slotNumber; 31 private String variableName; 32 33 34 public GeneralVariable() {}; 35 36 public void init( Expression select, 37 int nameCode) { 38 this.select = select; 39 this.nameCode = nameCode; 40 adoptChildExpression(select); 41 } 42 43 public void setSelectExpression(Expression select) { 44 this.select = select; 45 adoptChildExpression(select); 46 } 47 48 public Expression getSelectExpression() { 49 return select; 50 } 51 52 public void setRequiredType(SequenceType required) { 53 requiredType = required; 54 } 55 56 public SequenceType getRequiredType() { 57 return requiredType; 58 } 59 60 public void setNameCode(int nameCode) { 61 this.nameCode = nameCode; 62 } 63 64 public int getNameCode() { 65 return nameCode; 66 } 67 68 public void setAssignable(boolean assignable) { 69 if (assignable) { 70 properties |= ASSIGNABLE; 71 } else { 72 properties &= ~ASSIGNABLE; 73 } 74 } 75 76 public void setRequiredParam(boolean requiredParam) { 77 if (requiredParam) { 78 properties |= REQUIRED; 79 } else { 80 properties &= ~REQUIRED; 81 } 82 } 83 84 85 86 public void setTunnel(boolean tunnel) { 87 if (tunnel) { 88 properties |= TUNNEL; 89 } else { 90 properties &= ~TUNNEL; 91 } 92 } 93 94 99 100 public final boolean isAssignable() { 101 return (properties & ASSIGNABLE) != 0; 102 } 103 104 108 109 public int getVariableFingerprint() { 110 return nameCode & 0xfffff; 111 } 112 113 118 119 public ItemType getItemType() { 120 return NoNodeTest.getInstance(); 121 } 122 123 128 129 public int getCardinality() { 130 return StaticProperty.EMPTY; 131 } 132 133 public boolean isGlobal() { 134 return false; 135 } 136 137 141 142 public int getLocalSlotNumber() { 143 return slotNumber; 144 } 145 146 public final boolean isRequiredParam() { 147 return (properties & REQUIRED) != 0; 148 } 149 150 public final boolean isTunnelParam() { 151 return (properties & TUNNEL) != 0; 152 } 153 154 public int getInstructionNameCode() { 155 return StandardNames.XSL_VARIABLE; 156 } 157 158 public Expression simplify(StaticContext env) throws XPathException { 159 if (select != null) { 160 select = select.simplify(env); 161 } 162 return this; 163 } 164 165 public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException { 166 if (select != null) { 167 select = select.typeCheck(env, contextItemType); 168 adoptChildExpression(select); 169 } 170 checkAgainstRequiredType(env); 171 return this; 172 } 173 174 public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException { 175 if (select != null) { 176 select = select.optimize(opt, env, contextItemType); 177 adoptChildExpression(select); 178 } 179 return this; 180 } 181 182 183 188 189 private void checkAgainstRequiredType(StaticContext env) 190 throws XPathException { 191 RoleLocator role = new RoleLocator(RoleLocator.VARIABLE, variableName, 0, null); 193 role.setSourceLocator(this); 194 SequenceType r = requiredType; 195 if (r != null && select != null) { 196 select = TypeChecker.staticTypeCheck(select, requiredType, false, role, env); 198 } 199 } 200 201 215 216 public Item evaluateItem(XPathContext context) throws XPathException { 217 process(context); 218 return null; 219 } 220 221 237 238 public SequenceIterator iterate(XPathContext context) throws XPathException { 239 evaluateItem(context); 240 return EmptyIterator.getInstance(); 241 } 242 243 248 249 public ValueRepresentation getSelectValue(XPathContext context) throws XPathException { 250 if (select==null) { 251 throw new AssertionError ("*** No select expression!!"); 252 254 } else { 255 258 if (isAssignable()) { 259 return ExpressionTool.eagerEvaluate(select, context); 260 } else { 261 return ExpressionTool.lazyEvaluate(select, context, 10); 262 } 263 264 } 265 } 266 267 272 273 protected void promoteInst(PromotionOffer offer) throws XPathException { 274 if (select != null) { 275 select = doPromotion(select, offer); 276 } 277 } 278 279 284 285 286 public Iterator iterateSubExpressions() { 287 if (select != null) { 288 return new MonoIterator(select); 289 } else { 290 return Collections.EMPTY_LIST.iterator(); 291 } 292 } 293 294 301 302 public void display(int level, NamePool pool, PrintStream out) { 303 out.println(ExpressionTool.indent(level) + "variable " + 304 pool.getDisplayName(nameCode)); 305 if (select != null) { 306 select.display(level+1, pool, out); 307 } 308 } 309 310 public int getSlotNumber() { 311 return slotNumber; 312 } 313 314 public void setSlotNumber(int s) { 315 slotNumber = s; 316 } 317 318 public void setVariableName(String s) { 319 variableName = s; 320 } 321 322 public String getVariableName() { 323 return variableName; 324 } 325 } 326 327 | Popular Tags |