| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.pattern; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.StaticProperty; |
| 11 |
import net.sf.saxon.expr.XPathContext; |
| 12 |
import net.sf.saxon.expr.parser.ExpressionTool; |
| 13 |
import net.sf.saxon.expr.parser.RebindingMap; |
| 14 |
import net.sf.saxon.om.Item; |
| 15 |
import net.sf.saxon.om.NodeInfo; |
| 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.SchemaDeclaration; |
| 20 |
import net.sf.saxon.type.UType; |
| 21 |
import net.sf.saxon.value.SequenceType; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class NodeTestPattern extends Pattern { |
| 30 |
|
| 31 |
private NodeTest nodeTest; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public NodeTestPattern(NodeTest test) { |
| 41 |
nodeTest = test; |
| 42 |
setPriority(test.getDefaultPriority()); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public boolean matches(Item<?> item, XPathContext context) { |
| 55 |
return item instanceof NodeInfo && nodeTest.matchesNode((NodeInfo)item); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public NodeTest getItemType() { |
| 63 |
return nodeTest; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
@Override |
| 72 |
public UType getUType() { |
| 73 |
return nodeTest.getUType(); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public int getFingerprint() { |
| 84 |
return nodeTest.getFingerprint(); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public String toString() { |
| 92 |
return nodeTest.toString(); |
| 93 |
} |
| 94 |
|
| 95 |
public String toShortString() { |
| 96 |
return nodeTest.toShortString(); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public boolean equals( Object other) { |
| 106 |
return (other instanceof NodeTestPattern) && |
| 107 |
((NodeTestPattern) other).nodeTest.equals(nodeTest); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public int computeHashCode() { |
| 115 |
return 0x7aeffea8 ^ nodeTest.hashCode(); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
@Override |
| 127 |
public Pattern convertToTypedPattern(String val) throws XPathException { |
| 128 |
if (nodeTest instanceof NameTest && nodeTest.getUType() == UType.ELEMENT) { |
| 129 |
SchemaDeclaration decl = getConfiguration().getElementDeclaration(nodeTest.getMatchingNodeName()); |
| 130 |
if (decl == null) { |
| 131 |
if ("lax".equals(val)) { |
| 132 |
return this; |
| 133 |
} else { |
| 134 |
|
| 135 |
throw new XPathException("The mode specifies typed='strict', " + |
| 136 |
"but there is no schema element declaration named " + nodeTest, "XTSE3105"); |
| 137 |
} |
| 138 |
} else { |
| 139 |
NodeTest schemaNodeTest = decl.makeSchemaNodeTest(); |
| 140 |
return new NodeTestPattern(schemaNodeTest); |
| 141 |
} |
| 142 |
} else { |
| 143 |
return this; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
public void export(ExpressionPresenter presenter) throws XPathException { |
| 148 |
presenter.startElement("p.nodeTest"); |
| 149 |
presenter.emitAttribute("test", nodeTest.toString()); |
| 150 |
ExpressionPresenter.ExportOptions options = (ExpressionPresenter.ExportOptions) presenter.getOptions(); |
| 151 |
if ("JS".equals(options.target)) { |
| 152 |
int targetVersion = options.targetVersion; |
| 153 |
try { |
| 154 |
presenter.emitAttribute("jsTest", nodeTest.generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion)); |
| 155 |
} catch (XPathException e) { |
| 156 |
e.maybeSetLocation(getLocation()); |
| 157 |
throw e; |
| 158 |
} |
| 159 |
} |
| 160 |
if (options.addStaticType) { |
| 161 |
SequenceType type = SequenceType.makeSequenceType(nodeTest, StaticProperty.ALLOWS_ONE); |
| 162 |
presenter.emitAttribute("sType", type.toExportString2()); |
| 163 |
} |
| 164 |
presenter.endElement(); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public Pattern copy(RebindingMap rebindings) { |
| 176 |
NodeTestPattern n = new NodeTestPattern(nodeTest.copy()); |
| 177 |
n.setPriority(getDefaultPriority()); |
| 178 |
ExpressionTool.copyLocationInfo(this, n); |
| 179 |
return n; |
| 180 |
} |
| 181 |
|
| 182 |
public NodeTest getNodeTest() { |
| 183 |
return nodeTest; |
| 184 |
} |
| 185 |
} |
| 186 |
|