| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.pattern; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.XPathContext; |
| 11 |
import net.sf.saxon.expr.parser.ExpressionTool; |
| 12 |
import net.sf.saxon.expr.parser.RebindingMap; |
| 13 |
import net.sf.saxon.om.Item; |
| 14 |
import net.sf.saxon.om.NodeInfo; |
| 15 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.tree.util.Navigator; |
| 18 |
import net.sf.saxon.type.AnyItemType; |
| 19 |
import net.sf.saxon.type.ItemType; |
| 20 |
import net.sf.saxon.type.UType; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public final class SimplePositionalPattern extends Pattern { |
| 29 |
|
| 30 |
private NodeTest nodeTest; |
| 31 |
private int position; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public SimplePositionalPattern(NodeTest nodeTest, int position) { |
| 41 |
this.nodeTest = nodeTest; |
| 42 |
this.position = position; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public int getPosition() { |
| 52 |
return position; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public NodeTest getNodeTest() { |
| 60 |
return nodeTest; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public boolean matches(Item<?> item, XPathContext context) { |
| 70 |
return item instanceof NodeInfo && matchesBeneathAnchor((NodeInfo) item, null, context); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
@Override |
| 79 |
public UType getUType() { |
| 80 |
return nodeTest.getUType(); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public int getFingerprint() { |
| 91 |
return nodeTest.getFingerprint(); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public ItemType getItemType() { |
| 99 |
return nodeTest.getPrimitiveItemType(); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public boolean equals(Object other) { |
| 109 |
if (other instanceof SimplePositionalPattern) { |
| 110 |
SimplePositionalPattern fp = (SimplePositionalPattern) other; |
| 111 |
return nodeTest.equals(fp.nodeTest) && position == fp.position; |
| 112 |
} else { |
| 113 |
return false; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public int computeHashCode() { |
| 122 |
return nodeTest.hashCode() ^ (position<<3); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public boolean isMotionless() { |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public boolean matchesBeneathAnchor(NodeInfo node, NodeInfo anchor, XPathContext context) { |
| 150 |
if (!nodeTest.matchesNode(node)) { |
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
if (anchor != null && node.getParent() != anchor) { |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
return position == Navigator.getSiblingPosition(node, nodeTest, position); |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public Pattern copy(RebindingMap rebindings) { |
| 171 |
SimplePositionalPattern n = new SimplePositionalPattern(nodeTest.copy(), position); |
| 172 |
ExpressionTool.copyLocationInfo(this, n); |
| 173 |
return n; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
@Override |
| 180 |
public String toString() { |
| 181 |
return nodeTest + "[" + position + "]"; |
| 182 |
} |
| 183 |
|
| 184 |
public void export(ExpressionPresenter presenter) throws XPathException { |
| 185 |
presenter.startElement("p.simPos"); |
| 186 |
presenter.emitAttribute("test", nodeTest.toString()); |
| 187 |
if ("JS".equals(((ExpressionPresenter.ExportOptions) presenter.getOptions()).target)) { |
| 188 |
int targetVersion = ((ExpressionPresenter.ExportOptions) presenter.getOptions()).targetVersion; |
| 189 |
presenter.emitAttribute("jsTest", nodeTest.generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion)); |
| 190 |
} |
| 191 |
presenter.emitAttribute("pos", position + ""); |
| 192 |
presenter.endElement(); |
| 193 |
} |
| 194 |
|
| 195 |
} |
| 196 |
|