1 package net.sf.saxon.expr; 2 import net.sf.saxon.om.*; 3 import net.sf.saxon.trans.XPathException; 4 import net.sf.saxon.type.AnyItemType; 5 import net.sf.saxon.type.ItemType; 6 import net.sf.saxon.value.SequenceType; 7 import net.sf.saxon.value.Value; 8 import net.sf.saxon.value.SingletonNode; 9 10 import java.io.PrintStream ; 11 12 16 17 public class VariableReference extends ComputedExpression implements BindingReference { 18 19 Binding binding = null; SequenceType staticType = null; 22 Value constantValue = null; 23 transient String displayName = null; 24 25 29 30 public VariableReference(VariableDeclaration declaration) { 31 32 37 declaration.registerReference(this); 39 displayName = declaration.getVariableName(); 40 } 41 42 45 46 public Expression simplify(StaticContext env) { 47 return this; 48 } 49 50 56 57 public void setStaticType(SequenceType type, Value value, int properties) { 58 staticType = type; 60 constantValue = value; 61 staticProperties = (properties &~StaticProperty.CONTEXT_DOCUMENT_NODESET) | 64 type.getCardinality() | 65 getDependencies(); 66 } 67 68 72 73 public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException { 74 if (constantValue != null) { 75 binding = null; 76 return constantValue; 77 } 78 if (staticType==null) { 79 throw new IllegalStateException ("Variable $" + displayName + " has not been fixed up"); 80 } else { 81 return this; 82 } 83 } 84 85 89 90 public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException { 91 if (constantValue != null) { 92 binding = null; 93 return constantValue; 94 } 95 return this; 96 } 97 98 102 103 public void fixup(Binding binding) { 104 this.binding = binding; 106 resetStaticProperties(); 107 } 108 109 114 115 public ItemType getItemType() { 116 if (staticType==null) { 117 return AnyItemType.getInstance(); 118 } else { 119 return staticType.getPrimaryType(); 120 } 121 } 122 123 126 127 public int computeCardinality() { 128 if (staticType==null) { 129 return StaticProperty.ALLOWS_ZERO_OR_MORE; 130 } else { 131 return staticType.getCardinality(); 132 } 133 } 134 135 139 140 public int computeSpecialProperties() { 141 int p = super.computeSpecialProperties(); 142 if (binding==null || !binding.isAssignable()) { 143 p |= StaticProperty.NON_CREATIVE; 147 } 148 return p; 149 } 150 151 156 157 public boolean equals(Object other) { 158 return (other instanceof VariableReference && 159 binding == ((VariableReference)other).binding && 160 binding != null); 161 } 162 163 166 167 public int hashCode() { 168 return binding==null ? 73619830 : binding.hashCode(); 169 } 170 171 172 public int getIntrinsicDependencies() { 173 if (binding == null || !binding.isGlobal()) { 174 return StaticProperty.DEPENDS_ON_LOCAL_VARIABLES; 175 } else { 176 return 0; 177 } 178 } 179 180 183 184 public Expression promote(PromotionOffer offer) throws XPathException { 185 if (offer.action == PromotionOffer.INLINE_VARIABLE_REFERENCES) { 186 Expression exp = offer.accept(this); 187 if (exp != null) { 188 offer.accepted = true; 191 return exp; 192 } 193 } 194 return this; 195 } 196 197 202 203 public int getImplementationMethod() { 204 return EVALUATE_METHOD | ITERATE_METHOD | PROCESS_METHOD; 205 } 206 207 213 214 public SequenceIterator iterate(XPathContext c) throws XPathException { 215 ValueRepresentation actual = evaluateVariable(c); 216 return Value.getIterator(actual); 217 } 218 219 public Item evaluateItem(XPathContext c) throws XPathException { 220 ValueRepresentation actual = evaluateVariable(c); 221 if (actual instanceof Item) { 222 return (Item)actual; 223 } 224 return Value.asItem(actual, c); 225 } 226 227 public void process(XPathContext c) throws XPathException { 228 ValueRepresentation actual = evaluateVariable(c); 229 if (actual instanceof NodeInfo) { 230 actual = new SingletonNode((NodeInfo)actual); 231 } 232 ((Value)actual).process(c); 233 } 234 235 public ValueRepresentation evaluateVariable(XPathContext c) throws XPathException { 236 237 if (binding==null) { 238 throw new IllegalStateException ("Variable $" + displayName + " has not been fixed up"); 240 } 241 242 return binding.evaluateVariable(c); 243 } 244 245 248 249 public Binding getBinding() { 250 return binding; 251 } 252 253 256 257 public void display(int level, NamePool pool, PrintStream out) { 258 if (displayName != null) { 259 out.println(ExpressionTool.indent(level) + '$' + displayName); 260 } else { 261 out.println(ExpressionTool.indent(level) + "$(unbound variable)"); 262 } 263 } 264 } 265 266 | Popular Tags |