| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.pattern; |
| 9 |
|
| 10 |
import net.sf.saxon.om.AxisInfo; |
| 11 |
import net.sf.saxon.om.Item; |
| 12 |
import net.sf.saxon.om.NodeInfo; |
| 13 |
import net.sf.saxon.om.NodeName; |
| 14 |
import net.sf.saxon.trans.Err; |
| 15 |
import net.sf.saxon.trans.XPathException; |
| 16 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 17 |
import net.sf.saxon.type.*; |
| 18 |
|
| 19 |
import java.util.Optional; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class DocumentNodeTest extends NodeTest { |
| 30 |
|
| 31 |
|
| 32 |
private NodeTest elementTest; |
| 33 |
|
| 34 |
public DocumentNodeTest(NodeTest elementTest) { |
| 35 |
this.elementTest = elementTest; |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public UType getUType() { |
| 45 |
return UType.DOCUMENT; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
@Override |
| 64 |
public boolean matches(int nodeKind, NodeName name, SchemaType annotation) { |
| 65 |
if (nodeKind != Type.DOCUMENT) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
throw new UnsupportedOperationException("DocumentNodeTest doesn't support this method"); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public boolean matchesNode(NodeInfo node) { |
| 80 |
if (node.getNodeKind() != Type.DOCUMENT) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
AxisIterator iter = node.iterateAxis(AxisInfo.CHILD); |
| 84 |
|
| 85 |
|
| 86 |
boolean found = false; |
| 87 |
NodeInfo n; |
| 88 |
while ((n = iter.next()) != null) { |
| 89 |
int kind = n.getNodeKind(); |
| 90 |
if (kind == Type.TEXT) { |
| 91 |
return false; |
| 92 |
} else if (kind == Type.ELEMENT) { |
| 93 |
if (found) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
if (elementTest.matchesNode(n)) { |
| 97 |
found = true; |
| 98 |
} else { |
| 99 |
return false; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
return found; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public final double getDefaultPriority() { |
| 111 |
return elementTest.getDefaultPriority(); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public int getPrimitiveType() { |
| 121 |
return Type.DOCUMENT; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public NodeTest getElementTest() { |
| 131 |
return elementTest; |
| 132 |
} |
| 133 |
|
| 134 |
public String toString() { |
| 135 |
return "document-node(" + elementTest + ')'; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public int hashCode() { |
| 143 |
return elementTest.hashCode() ^ 12345; |
| 144 |
} |
| 145 |
|
| 146 |
public boolean equals( Object other) { |
| 147 |
return other instanceof DocumentNodeTest && |
| 148 |
((DocumentNodeTest) other).elementTest.equals(elementTest); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
@Override |
| 161 |
public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) throws XPathException { |
| 162 |
String elTest = "function e(item) {" + elementTest.generateJavaScriptItemTypeTest(NodeKindTest.ELEMENT, targetVersion) + "};"; |
| 163 |
if (targetVersion == 1) { |
| 164 |
return elTest + "return SaxonJS.U.isNode(item) && (item.nodeType===9 || item.nodeType===11) && " + |
| 165 |
"SaxonJS.U.Axis.child(item).filter(e).next();"; |
| 166 |
} else { |
| 167 |
return elTest + "return SaxonJS.U.isConstrainedDocumentNode(item, e);"; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
@Override |
| 181 |
public Optional<String> explainMismatch(Item item, TypeHierarchy th) { |
| 182 |
Optional<String> explanation = super.explainMismatch(item, th); |
| 183 |
if (explanation.isPresent()) { |
| 184 |
return explanation; |
| 185 |
} |
| 186 |
NodeInfo node = (NodeInfo) item; |
| 187 |
AxisIterator iter = node.iterateAxis(AxisInfo.CHILD); |
| 188 |
|
| 189 |
|
| 190 |
boolean found = false; |
| 191 |
NodeInfo n; |
| 192 |
while ((n = iter.next()) != null) { |
| 193 |
int kind = n.getNodeKind(); |
| 194 |
if (kind == Type.TEXT) { |
| 195 |
return Optional.of("The supplied document node has text node children"); |
| 196 |
} else if (kind == Type.ELEMENT) { |
| 197 |
if (found) { |
| 198 |
return Optional.of("The supplied document node has more than one element child"); |
| 199 |
} |
| 200 |
if (elementTest.matchesNode(n)) { |
| 201 |
found = true; |
| 202 |
} else { |
| 203 |
String s = "The supplied document node has an element child (" + Err.depict(n) + |
| 204 |
") that does not satisfy the element test"; |
| 205 |
Optional<String> more = elementTest.explainMismatch(n, th); |
| 206 |
if (more.isPresent()) { |
| 207 |
s += ". " + more.get(); |
| 208 |
} |
| 209 |
return Optional.of(s); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
return Optional.empty(); |
| 214 |
} |
| 215 |
|
| 216 |
} |
| 217 |
|