| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.ma.map; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Expression; |
| 11 |
import net.sf.saxon.expr.StaticProperty; |
| 12 |
import net.sf.saxon.expr.parser.RoleDiagnostic; |
| 13 |
import net.sf.saxon.om.Genre; |
| 14 |
import net.sf.saxon.om.GroundedValue; |
| 15 |
import net.sf.saxon.om.Item; |
| 16 |
import net.sf.saxon.om.Sequence; |
| 17 |
import net.sf.saxon.trans.Err; |
| 18 |
import net.sf.saxon.trans.XPathException; |
| 19 |
import net.sf.saxon.tree.iter.AtomicIterator; |
| 20 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 21 |
import net.sf.saxon.type.*; |
| 22 |
import net.sf.saxon.value.AtomicValue; |
| 23 |
import net.sf.saxon.value.Cardinality; |
| 24 |
import net.sf.saxon.value.SequenceType; |
| 25 |
import net.sf.saxon.value.StringValue; |
| 26 |
|
| 27 |
import java.util.*; |
| 28 |
import java.util.function.Function; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class TupleItemType extends AnyFunctionType implements TupleType { |
| 39 |
|
| 40 |
private Map<String, SequenceType> fields = new HashMap<>(); |
| 41 |
private Set<String> optionalFields; |
| 42 |
private boolean extensible; |
| 43 |
|
| 44 |
public TupleItemType(List<String> names, List<SequenceType> types) { |
| 45 |
for (int i = 0; i < names.size(); i++) { |
| 46 |
fields.put(names.get(i), types.get(i)); |
| 47 |
} |
| 48 |
this.optionalFields = Collections.emptySet(); |
| 49 |
} |
| 50 |
|
| 51 |
public TupleItemType(List<String> names, List<SequenceType> types, Set<String> optionalFields, boolean extensible) { |
| 52 |
for (int i=0; i<names.size(); i++) { |
| 53 |
fields.put(names.get(i), types.get(i)); |
| 54 |
} |
| 55 |
this.optionalFields = optionalFields; |
| 56 |
this.extensible = extensible; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
@Override |
| 65 |
public Genre getGenre() { |
| 66 |
return Genre.MAP; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
@Override |
| 76 |
public boolean isMapType() { |
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
@Override |
| 87 |
public boolean isArrayType() { |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
@Override |
| 97 |
public Iterable<String> getFieldNames() { |
| 98 |
return fields.keySet(); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public SequenceType getFieldType(String field) { |
| 108 |
return fields.get(field); |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public boolean isOptional(String field) { |
| 113 |
return optionalFields.contains(field); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
@Override |
| 122 |
public boolean isExtensible() { |
| 123 |
return extensible; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
@Override |
| 134 |
public boolean matches(Item item, TypeHierarchy th) throws XPathException { |
| 135 |
if (!(item instanceof MapItem)) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
MapItem map = (MapItem)item; |
| 139 |
for (Map.Entry<String, SequenceType> field : fields.entrySet()) { |
| 140 |
Sequence<?> val = map.get(new StringValue(field.getKey())); |
| 141 |
if (val == null) { |
| 142 |
if (!optionalFields.contains(field.getKey())) { |
| 143 |
return false; |
| 144 |
} |
| 145 |
} else if (!field.getValue().matches(val, th)) { |
| 146 |
return false; |
| 147 |
} |
| 148 |
} |
| 149 |
if (!extensible) { |
| 150 |
AtomicIterator keyIter = map.keys(); |
| 151 |
AtomicValue key; |
| 152 |
while ((key = keyIter.next()) != null) { |
| 153 |
if (!(key instanceof StringValue) || !fields.containsKey(key.getStringValue())) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
return true; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public int getArity() { |
| 168 |
return 1; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public SequenceType[] getArgumentTypes() { |
| 178 |
|
| 179 |
return new SequenceType[]{SequenceType.SINGLE_ATOMIC}; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public SequenceType getResultType() { |
| 189 |
if (extensible) { |
| 190 |
return SequenceType.ANY_SEQUENCE; |
| 191 |
} else { |
| 192 |
ItemType resultType = null; |
| 193 |
boolean allowsMany = false; |
| 194 |
for (Map.Entry<String, SequenceType> field : fields.entrySet()) { |
| 195 |
if (resultType == null) { |
| 196 |
resultType = field.getValue().getPrimaryType(); |
| 197 |
} else { |
| 198 |
resultType = Type.getCommonSuperType(resultType, field.getValue().getPrimaryType()); |
| 199 |
} |
| 200 |
allowsMany = allowsMany || Cardinality.allowsMany(field.getValue().getCardinality()); |
| 201 |
} |
| 202 |
return SequenceType.makeSequenceType(resultType, |
| 203 |
allowsMany ? StaticProperty.ALLOWS_ZERO_OR_MORE : StaticProperty.ALLOWS_ZERO_OR_ONE); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public String toString() { |
| 214 |
return makeString(SequenceType::toString); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
@Override |
| 226 |
public String toExportString() { |
| 227 |
return makeString(SequenceType::toExportString); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
private String makeString(Function<SequenceType, String> show) { |
| 237 |
FastStringBuffer sb = new FastStringBuffer(100); |
| 238 |
sb.append("tuple("); |
| 239 |
boolean first = true; |
| 240 |
for (Map.Entry<String, SequenceType> field : fields.entrySet()) { |
| 241 |
if (first) { |
| 242 |
first = false; |
| 243 |
} else { |
| 244 |
sb.append(", "); |
| 245 |
} |
| 246 |
sb.append(field.getKey()); |
| 247 |
if (isOptional(field.getKey())) { |
| 248 |
sb.append("?"); |
| 249 |
} |
| 250 |
sb.append(": "); |
| 251 |
sb.append(show.apply(field.getValue())); |
| 252 |
} |
| 253 |
if (isExtensible()) { |
| 254 |
sb.append(", *"); |
| 255 |
} |
| 256 |
sb.append(")"); |
| 257 |
return sb.toString(); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
public boolean equals(Object other) { |
| 265 |
return this == other || other instanceof TupleItemType && fields.equals(((TupleItemType) other).fields); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
@Override |
| 272 |
public int hashCode() { |
| 273 |
return fields.hashCode(); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
public int relationship(FunctionItemType other, TypeHierarchy th) { |
| 283 |
if (other == AnyFunctionType.getInstance()) { |
| 284 |
return TypeHierarchy.SUBSUMED_BY; |
| 285 |
} else if (equals(other)) { |
| 286 |
return TypeHierarchy.SAME_TYPE; |
| 287 |
} else if (other == MapType.ANY_MAP_TYPE) { |
| 288 |
return TypeHierarchy.SUBSUMED_BY; |
| 289 |
} else if (other.isArrayType()) { |
| 290 |
return TypeHierarchy.DISJOINT; |
| 291 |
} else if (other instanceof MapType) { |
| 292 |
MapType f2 = (MapType) other; |
| 293 |
int keyRel = th.relationship(BuiltInAtomicType.STRING, f2.getKeyType()); |
| 294 |
if (keyRel == TypeHierarchy.DISJOINT) { |
| 295 |
return TypeHierarchy.DISJOINT; |
| 296 |
} |
| 297 |
|
| 298 |
if (f2.getValueType().getPrimaryType().equals(AnyItemType.getInstance()) && f2.getValueType().getCardinality() == StaticProperty.ALLOWS_ZERO_OR_MORE) { |
| 299 |
return TypeHierarchy.SUBSUMED_BY; |
| 300 |
} else { |
| 301 |
|
| 302 |
for (SequenceType entry : fields.values()) { |
| 303 |
int rel = th.sequenceTypeRelationship(entry, f2.getValueType()); |
| 304 |
if (!(rel == TypeHierarchy.SUBSUMED_BY || rel == TypeHierarchy.SAME_TYPE)) { |
| 305 |
return TypeHierarchy.OVERLAPS; |
| 306 |
} |
| 307 |
} |
| 308 |
return TypeHierarchy.SUBSUMED_BY; |
| 309 |
} |
| 310 |
} else { |
| 311 |
int rel; |
| 312 |
rel = new SpecificFunctionType(getArgumentTypes(), getResultType()).relationship(other, th); |
| 313 |
return rel; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
@Override |
| 326 |
public Optional<String> explainMismatch(Item item, TypeHierarchy th) { |
| 327 |
if (item instanceof MapItem) { |
| 328 |
for (Map.Entry<String, SequenceType> entry : fields.entrySet()) { |
| 329 |
String key = entry.getKey(); |
| 330 |
SequenceType required = entry.getValue(); |
| 331 |
GroundedValue value = ((MapItem) item).get(new StringValue(key)); |
| 332 |
if (value == null) { |
| 333 |
if (!optionalFields.contains(key)) { |
| 334 |
return Optional.of("Field " + key + " is absent, but is not declared optional"); |
| 335 |
} |
| 336 |
} else { |
| 337 |
try { |
| 338 |
if (!required.matches(value, th)) { |
| 339 |
String s = "Field " + key + " has value " |
| 340 |
+ Err.depictSequence(value) |
| 341 |
+ " which does not match the required type " |
| 342 |
+ required.toString(); |
| 343 |
Optional<String> more = required.explainMismatch(value, th); |
| 344 |
if (more.isPresent()) { |
| 345 |
s += ". " + more.get(); |
| 346 |
} |
| 347 |
return Optional.of(s); |
| 348 |
} |
| 349 |
} catch (XPathException err) { |
| 350 |
|
| 351 |
return Optional.empty(); |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
if (!extensible) { |
| 356 |
AtomicIterator keyIter = ((MapItem)item).keys(); |
| 357 |
AtomicValue key; |
| 358 |
while ((key = keyIter.next()) != null) { |
| 359 |
if (!(key instanceof StringValue)) { |
| 360 |
return Optional.of("Undeclared field " + key + " is present, but it is not a string, and the tuple type is not extensible"); |
| 361 |
} else if (!fields.containsKey(key.getStringValue())) { |
| 362 |
return Optional.of("Undeclared field " + key + " is present, but the tuple type is not extensible"); |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
} |
| 367 |
return Optional.empty(); |
| 368 |
} |
| 369 |
|
| 370 |
@Override |
| 371 |
public Expression makeFunctionSequenceCoercer(Expression exp, RoleDiagnostic role) |
| 372 |
throws XPathException { |
| 373 |
return new SpecificFunctionType(getArgumentTypes(), getResultType()).makeFunctionSequenceCoercer(exp, role); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
@Override |
| 388 |
public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) throws XPathException { |
| 389 |
throw new UnsupportedOperationException(); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
|