| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.pattern; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.functions.Nilled_1; |
| 12 |
import net.sf.saxon.om.Item; |
| 13 |
import net.sf.saxon.om.NodeInfo; |
| 14 |
import net.sf.saxon.om.NodeName; |
| 15 |
import net.sf.saxon.trans.SaxonErrorCode; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.tree.tiny.NodeVectorTree; |
| 18 |
import net.sf.saxon.tree.tiny.TinyTree; |
| 19 |
import net.sf.saxon.type.*; |
| 20 |
|
| 21 |
import java.util.Optional; |
| 22 |
import java.util.function.IntPredicate; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class ContentTypeTest extends NodeTest { |
| 33 |
|
| 34 |
private int kind; |
| 35 |
private SchemaType schemaType; |
| 36 |
private Configuration config; |
| 37 |
private boolean nillable = false; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public ContentTypeTest(int nodeKind, SchemaType schemaType, Configuration config, boolean nillable) { |
| 49 |
this.kind = nodeKind; |
| 50 |
this.schemaType = schemaType; |
| 51 |
this.config = config; |
| 52 |
this.nillable = nillable; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public UType getUType() { |
| 62 |
return kind == Type.ELEMENT ? UType.ELEMENT : UType.ATTRIBUTE; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public void setNillable(boolean nillable) { |
| 71 |
this.nillable = nillable; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public boolean isNillable() { |
| 81 |
return nillable; |
| 82 |
} |
| 83 |
|
| 84 |
public SchemaType getSchemaType() { |
| 85 |
return schemaType; |
| 86 |
} |
| 87 |
|
| 88 |
public int getNodeKind() { |
| 89 |
return kind; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
@Override |
| 108 |
public boolean matches(int nodeKind, NodeName name, SchemaType annotation) { |
| 109 |
return kind == nodeKind && matchesAnnotation(annotation); |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public IntPredicate getMatcher(final NodeVectorTree tree) { |
| 114 |
final byte[] nodeKindArray = tree.getNodeKindArray(); |
| 115 |
return nodeNr -> (nodeKindArray[nodeNr]&0x0f) == kind && |
| 116 |
matchesAnnotation(((TinyTree) tree).getSchemaType(nodeNr)) && |
| 117 |
(nillable || !((TinyTree) tree).isNilled(nodeNr)); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public boolean matchesNode( NodeInfo node) { |
| 129 |
return node.getNodeKind() == kind && |
| 130 |
matchesAnnotation(node.getSchemaType()) |
| 131 |
&& (nillable || !Nilled_1.isNilled(node)); |
| 132 |
} |
| 133 |
|
| 134 |
private boolean matchesAnnotation(SchemaType annotation) { |
| 135 |
if (annotation == null) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
if (schemaType == AnyType.getInstance()) { |
| 139 |
return true; |
| 140 |
} |
| 141 |
|
| 142 |
if (annotation.equals(schemaType)) { |
| 143 |
return true; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
int r = config.getTypeHierarchy().schemaTypeRelationship(annotation, schemaType); |
| 148 |
return r == TypeHierarchy.SAME_TYPE || r == TypeHierarchy.SUBSUMED_BY; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public final double getDefaultPriority() { |
| 156 |
return 0; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public int getPrimitiveType() { |
| 166 |
return kind; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public SchemaType getContentType() { |
| 175 |
return schemaType; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public AtomicType getAtomizedItemType() { |
| 185 |
SchemaType type = schemaType; |
| 186 |
try { |
| 187 |
if (type.isAtomicType()) { |
| 188 |
return (AtomicType) type; |
| 189 |
} else if (type instanceof ListType) { |
| 190 |
SimpleType mem = ((ListType) type).getItemType(); |
| 191 |
if (mem.isAtomicType()) { |
| 192 |
return (AtomicType) mem; |
| 193 |
} |
| 194 |
} else if (type instanceof ComplexType && ((ComplexType) type).isSimpleContent()) { |
| 195 |
SimpleType ctype = ((ComplexType) type).getSimpleContentType(); |
| 196 |
assert ctype != null; |
| 197 |
if (ctype.isAtomicType()) { |
| 198 |
return (AtomicType) ctype; |
| 199 |
} else if (ctype instanceof ListType) { |
| 200 |
SimpleType mem = ((ListType) ctype).getItemType(); |
| 201 |
if (mem.isAtomicType()) { |
| 202 |
return (AtomicType) mem; |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
} catch (MissingComponentException e) { |
| 207 |
return BuiltInAtomicType.ANY_ATOMIC; |
| 208 |
} |
| 209 |
return BuiltInAtomicType.ANY_ATOMIC; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
public boolean isAtomizable(TypeHierarchy th) { |
| 221 |
return !(schemaType.isComplexType() && |
| 222 |
((ComplexType) schemaType).getVariety() == ComplexType.VARIETY_ELEMENT_ONLY); |
| 223 |
} |
| 224 |
|
| 225 |
public String toString() { |
| 226 |
return (kind == Type.ELEMENT ? "element(*, " : "attribute(*, ") + |
| 227 |
schemaType.getEQName() + ')'; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
@Override |
| 240 |
public String toExportString() { |
| 241 |
return (kind == Type.ELEMENT ? "element(*, " : "attribute(*, ") + |
| 242 |
schemaType.getNearestNamedType().getEQName() + ')'; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
public int hashCode() { |
| 250 |
return kind << 20 ^ schemaType.hashCode(); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public boolean equals(Object other) { |
| 257 |
return other instanceof ContentTypeTest && |
| 258 |
((ContentTypeTest) other).kind == kind && |
| 259 |
((ContentTypeTest) other).schemaType == schemaType && |
| 260 |
((ContentTypeTest) other).nillable == nillable; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
@Override |
| 277 |
public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) throws XPathException { |
| 278 |
if (kind == Type.ELEMENT) { |
| 279 |
if (schemaType == Untyped.getInstance() || schemaType == AnyType.getInstance()) { |
| 280 |
return NodeKindTest.makeNodeKindTest(getNodeKind()).generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion); |
| 281 |
} else if (schemaType == BuiltInAtomicType.UNTYPED_ATOMIC || schemaType == BuiltInAtomicType.ANY_ATOMIC) { |
| 282 |
return "return false;"; |
| 283 |
} |
| 284 |
} else if (kind == Type.ATTRIBUTE) { |
| 285 |
if (schemaType == Untyped.getInstance() || schemaType == AnyType.getInstance() |
| 286 |
|| schemaType == BuiltInAtomicType.UNTYPED_ATOMIC || schemaType == BuiltInAtomicType.ANY_ATOMIC) { |
| 287 |
return NodeKindTest.makeNodeKindTest(getNodeKind()).generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion); |
| 288 |
} |
| 289 |
} |
| 290 |
throw new XPathException("Cannot generate JS code to test type annotations", SaxonErrorCode.SXJS0001); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
@Override |
| 303 |
public Optional<String> explainMismatch(Item item, TypeHierarchy th) { |
| 304 |
Optional<String> explanation = super.explainMismatch(item, th); |
| 305 |
if (explanation.isPresent()) { |
| 306 |
return explanation; |
| 307 |
} |
| 308 |
NodeInfo node = (NodeInfo) item; |
| 309 |
if (!matchesAnnotation(((NodeInfo)item).getSchemaType())) { |
| 310 |
if (node.getSchemaType() == Untyped.getInstance()) { |
| 311 |
return Optional.of("The supplied node has not been schema-validated"); |
| 312 |
} |
| 313 |
if (node.getSchemaType() == BuiltInAtomicType.UNTYPED_ATOMIC) { |
| 314 |
return Optional.of("The supplied node has not been schema-validated"); |
| 315 |
} |
| 316 |
return Optional.of("The supplied node has the wrong type annotation (" + node.getSchemaType().getDescription() + ")"); |
| 317 |
} |
| 318 |
if (Nilled_1.isNilled(node) && !nillable) { |
| 319 |
return Optional.of("The supplied node has xsi:nil='true', which the required type does not allow"); |
| 320 |
} |
| 321 |
return Optional.empty(); |
| 322 |
} |
| 323 |
|
| 324 |
} |
| 325 |
|