1 package com.icl.saxon.expr; 2 import com.icl.saxon.Context; 3 import com.icl.saxon.Binding; 4 import com.icl.saxon.Bindery; 5 import com.icl.saxon.trace.*; import com.icl.saxon.style.XSLGeneralVariable; 7 import javax.xml.transform.TransformerException ; 8 9 12 13 public class VariableReference extends Expression { 14 15 int fingerprint; 16 Binding binding; 17 18 22 23 public VariableReference(int fingerprint, StaticContext staticContext) throws XPathException { 24 this.fingerprint = fingerprint; 25 binding = staticContext.bindVariable(fingerprint); 26 } 27 28 33 34 public int getDependencies() { 35 return Context.VARIABLES; 36 } 37 38 46 47 public Expression reduce(int dependencies, Context context) throws XPathException { 48 if ((dependencies & Context.VARIABLES) != 0) { 49 return evaluate(context); 50 } else { 51 return this; 52 } 53 } 54 55 61 62 63 public Value evaluate(Context c) throws XPathException { 64 65 Bindery b = c.getBindery(); 66 Value v = b.getValue(binding); 67 68 if (v==null) { 69 70 if (!binding.isGlobal()) { 71 throw new XPathException("Variable " + binding.getVariableName() + " is undefined"); 72 } 73 74 78 try { 79 80 b.setExecuting(binding, true); 81 82 if (binding instanceof XSLGeneralVariable) { 83 if (c.getController().isTracing()) { TraceListener listener = c.getController().getTraceListener(); 85 86 listener.enter((XSLGeneralVariable)binding, c); 87 ((XSLGeneralVariable)binding).process(c); 88 listener.leave((XSLGeneralVariable)binding, c); 89 90 } else { 91 ((XSLGeneralVariable)binding).process(c); 92 } 93 } 94 95 b.setExecuting(binding, false); 96 97 v = b.getValue(binding); 98 99 } catch (TransformerException err) { 100 if (err instanceof XPathException) { 101 throw (XPathException)err; 102 } else { 103 throw new XPathException(err); 104 } 105 } 106 107 if (v==null) { 108 throw new XPathException("Variable " + binding.getVariableName() + " is undefined"); 109 } 110 } 111 return v; 112 } 113 114 115 118 119 public Binding getBinding() { 120 return binding; 121 } 122 123 128 129 public int getDataType() { 130 return binding.getDataType(); 131 } 132 133 137 138 public Expression simplify() { 139 Value v = binding.constantValue(); 140 if (v==null) { 141 return this; 142 } else { 143 return v; 144 } 145 } 146 147 150 151 public void display(int level) { 152 System.err.println(indent(level) + "$" + binding.getVariableName()); 153 } 154 } 155 156 | Popular Tags |