| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.instruct.GlobalVariable; |
| 11 |
import net.sf.saxon.expr.parser.RebindingMap; |
| 12 |
import net.sf.saxon.om.Sequence; |
| 13 |
import net.sf.saxon.om.StandardNames; |
| 14 |
import net.sf.saxon.om.StructuredQName; |
| 15 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 16 |
import net.sf.saxon.trans.SymbolicName; |
| 17 |
import net.sf.saxon.trans.Visibility; |
| 18 |
import net.sf.saxon.trans.XPathException; |
| 19 |
|
| 20 |
import java.util.HashSet; |
| 21 |
import java.util.Set; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class GlobalVariableReference extends VariableReference implements ComponentInvocation { |
| 27 |
|
| 28 |
int bindingSlot = -1; |
| 29 |
|
| 30 |
public GlobalVariableReference(StructuredQName name) { |
| 31 |
super(name); |
| 32 |
} |
| 33 |
|
| 34 |
public GlobalVariableReference(GlobalVariable var) { |
| 35 |
super(var); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
public Expression copy(RebindingMap rebindings) { |
| 40 |
if (binding == null) { |
| 41 |
|
| 42 |
throw new UnsupportedOperationException("Cannot copy a variable reference whose binding is unknown"); |
| 43 |
} |
| 44 |
GlobalVariableReference ref = new GlobalVariableReference(getVariableName()); |
| 45 |
ref.copyFrom(this); |
| 46 |
return ref; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public void setBindingSlot(int slot) { |
| 58 |
if (bindingSlot != -1) { |
| 59 |
throw new AssertionError("Duplicate binding slot assignment"); |
| 60 |
} |
| 61 |
bindingSlot = slot; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public int getBindingSlot() { |
| 72 |
return bindingSlot; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public SymbolicName getSymbolicName() { |
| 81 |
return new SymbolicName(StandardNames.XSL_VARIABLE, getVariableName()); |
| 82 |
} |
| 83 |
|
| 84 |
public void setTarget(Component target) { |
| 85 |
binding = (GlobalVariable) target.getActor(); |
| 86 |
} |
| 87 |
|
| 88 |
public Component getTarget() { |
| 89 |
return ((GlobalVariable) binding).getDeclaringComponent(); |
| 90 |
} |
| 91 |
|
| 92 |
public Component getFixedTarget() { |
| 93 |
Component c = getTarget(); |
| 94 |
Visibility v = c.getVisibility(); |
| 95 |
if (v == Visibility.PRIVATE || v == Visibility.FINAL) { |
| 96 |
return c; |
| 97 |
} else { |
| 98 |
return null; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
@Override |
| 110 |
public Sequence<?> evaluateVariable(XPathContext c) throws XPathException { |
| 111 |
|
| 112 |
if (bindingSlot >= 0) { |
| 113 |
if (c.getCurrentComponent() == null) { |
| 114 |
throw new AssertionError("No current component"); |
| 115 |
} |
| 116 |
Component target = c.getTargetComponent(bindingSlot); |
| 117 |
if (target.isHiddenAbstractComponent()) { |
| 118 |
XPathException err = new XPathException("Cannot evaluate an abstract variable (" |
| 119 |
+ getVariableName().getDisplayName() |
| 120 |
+ ") with no overriding declaration", "XTDE3052"); |
| 121 |
err.setLocation(getLocation()); |
| 122 |
throw err; |
| 123 |
} |
| 124 |
GlobalVariable p = (GlobalVariable) target.getActor(); |
| 125 |
return p.evaluateVariable(c, target); |
| 126 |
} else { |
| 127 |
|
| 128 |
GlobalVariable b = (GlobalVariable) binding; |
| 129 |
return b.evaluateVariable(c, b.getDeclaringComponent()); |
| 130 |
} |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public void export(ExpressionPresenter out) throws XPathException { |
| 140 |
out.startElement("gVarRef", this); |
| 141 |
out.emitAttribute("name", getVariableName()); |
| 142 |
out.emitAttribute("bSlot", "" + getBindingSlot()); |
| 143 |
out.endElement(); |
| 144 |
} |
| 145 |
|
| 146 |
public Set<Expression> getPreconditions() { |
| 147 |
Set<Expression> pre = new HashSet<Expression>(); |
| 148 |
|
| 149 |
return pre; |
| 150 |
} |
| 151 |
} |