| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.xpath; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.*; |
| 12 |
import net.sf.saxon.expr.parser.RebindingMap; |
| 13 |
import net.sf.saxon.om.Sequence; |
| 14 |
import net.sf.saxon.om.SequenceIterator; |
| 15 |
import net.sf.saxon.om.StructuredQName; |
| 16 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.type.AnyItemType; |
| 19 |
import net.sf.saxon.type.ItemType; |
| 20 |
import net.sf.saxon.value.EmptySequence; |
| 21 |
|
| 22 |
import javax.xml.xpath.XPathVariableResolver; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class JAXPVariableReference extends Expression implements Callable { |
| 35 |
|
| 36 |
private StructuredQName name; |
| 37 |
private XPathVariableResolver resolver; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public JAXPVariableReference(StructuredQName name, XPathVariableResolver resolver) { |
| 45 |
this.name = name; |
| 46 |
this.resolver = resolver; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public String getExpressionName() { |
| 58 |
return "$" + name.getDisplayName(); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public Expression copy(RebindingMap rebindings) { |
| 70 |
return new JAXPVariableReference(name, resolver); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public ItemType getItemType() { |
| 79 |
return AnyItemType.getInstance(); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public int computeCardinality() { |
| 87 |
return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public int computeSpecialProperties() { |
| 97 |
return StaticProperty.NO_NODES_NEWLY_CREATED; |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public int getImplementationMethod() { |
| 102 |
return ITERATE_METHOD; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public boolean equals(Object other) { |
| 110 |
return other instanceof JAXPVariableReference && |
| 111 |
((JAXPVariableReference)other).name.equals(name) && |
| 112 |
((JAXPVariableReference)other).resolver == resolver; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public int computeHashCode() { |
| 120 |
return name.hashCode(); |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 125 |
Configuration config = context.getConfiguration(); |
| 126 |
Object value = resolver.resolveVariable(name.toJaxpQName()); |
| 127 |
if (value == null) { |
| 128 |
return EmptySequence.getInstance(); |
| 129 |
} |
| 130 |
JPConverter converter = JPConverter.allocate(value.getClass(), null, config); |
| 131 |
return converter.convert(value, context); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public SequenceIterator<?> iterate(XPathContext context) throws XPathException { |
| 140 |
return call(context, null).iterate(); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public String toString() { |
| 150 |
return getExpressionName(); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public void export(ExpressionPresenter destination) throws XPathException { |
| 159 |
destination.startElement("jaxpVar", this); |
| 160 |
destination.emitAttribute("name", name); |
| 161 |
destination.endElement(); |
| 162 |
} |
| 163 |
|
| 164 |
} |
| 165 |
|