| 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.Item; |
| 15 |
import net.sf.saxon.trans.Err; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 18 |
import net.sf.saxon.type.*; |
| 19 |
import net.sf.saxon.value.Cardinality; |
| 20 |
import net.sf.saxon.value.SequenceType; |
| 21 |
|
| 22 |
import java.util.Optional; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class MapType extends AnyFunctionType { |
| 29 |
|
| 30 |
public final static MapType ANY_MAP_TYPE = new MapType(BuiltInAtomicType.ANY_ATOMIC, SequenceType.ANY_SEQUENCE); |
| 31 |
|
| 32 |
|
| 33 |
public final static MapType EMPTY_MAP_TYPE = new MapType(BuiltInAtomicType.ANY_ATOMIC, SequenceType.ANY_SEQUENCE, true); |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public static final SequenceType OPTIONAL_MAP_ITEM = |
| 39 |
SequenceType.makeSequenceType(ANY_MAP_TYPE, StaticProperty.ALLOWS_ZERO_OR_ONE); |
| 40 |
public static final SequenceType SINGLE_MAP_ITEM = |
| 41 |
SequenceType.makeSequenceType(ANY_MAP_TYPE, StaticProperty.ALLOWS_ONE); |
| 42 |
public static final SequenceType SEQUENCE_OF_MAPS = |
| 43 |
SequenceType.makeSequenceType(ANY_MAP_TYPE, StaticProperty.ALLOWS_ZERO_OR_MORE); |
| 44 |
|
| 45 |
private AtomicType keyType; |
| 46 |
private SequenceType valueType; |
| 47 |
private boolean mustBeEmpty; |
| 48 |
|
| 49 |
public MapType(AtomicType keyType, SequenceType valueType) { |
| 50 |
this.keyType = keyType; |
| 51 |
this.valueType = valueType; |
| 52 |
this.mustBeEmpty = false; |
| 53 |
} |
| 54 |
|
| 55 |
public MapType(AtomicType keyType, SequenceType valueType, boolean mustBeEmpty) { |
| 56 |
this.keyType = keyType; |
| 57 |
this.valueType = valueType; |
| 58 |
this.mustBeEmpty = mustBeEmpty; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
@Override |
| 67 |
public Genre getGenre() { |
| 68 |
return Genre.MAP; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public AtomicType getKeyType() { |
| 77 |
return keyType; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public SequenceType getValueType() { |
| 86 |
return valueType; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
@Override |
| 96 |
public boolean isMapType() { |
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
@Override |
| 107 |
public boolean isArrayType() { |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
@Override |
| 119 |
public String getAlphaCode() { |
| 120 |
return "FM"; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public boolean isAtomizable(TypeHierarchy th) { |
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
@Override |
| 143 |
public boolean matches(Item item, TypeHierarchy th) { |
| 144 |
if (!(item instanceof MapItem)){ |
| 145 |
return false; |
| 146 |
} |
| 147 |
if (((MapItem) item).isEmpty()) { |
| 148 |
return true; |
| 149 |
} else if (mustBeEmpty) { |
| 150 |
return false; |
| 151 |
} |
| 152 |
if (this == ANY_MAP_TYPE) { |
| 153 |
return true; |
| 154 |
} else { |
| 155 |
return ((MapItem)item).conforms(keyType, valueType, th); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public int getArity() { |
| 166 |
return 1; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public SequenceType[] getArgumentTypes() { |
| 176 |
|
| 177 |
return new SequenceType[]{SequenceType.makeSequenceType(BuiltInAtomicType.ANY_ATOMIC, StaticProperty.EXACTLY_ONE)}; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public SequenceType getResultType() { |
| 187 |
|
| 188 |
if (Cardinality.allowsZero(valueType.getCardinality())) { |
| 189 |
return valueType; |
| 190 |
} else { |
| 191 |
return SequenceType.makeSequenceType( |
| 192 |
valueType.getPrimaryType(), Cardinality.union(valueType.getCardinality(), StaticProperty.ALLOWS_ZERO)); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public String toString() { |
| 203 |
if (this == ANY_MAP_TYPE) { |
| 204 |
return "map(*)"; |
| 205 |
} else if (this == EMPTY_MAP_TYPE) { |
| 206 |
return "map(\u00B0)"; |
| 207 |
} else { |
| 208 |
FastStringBuffer sb = new FastStringBuffer(100); |
| 209 |
sb.append("map("); |
| 210 |
sb.append(keyType.toString()); |
| 211 |
sb.append(", "); |
| 212 |
sb.append(valueType.toString()); |
| 213 |
sb.append(")"); |
| 214 |
return sb.toString(); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
@Override |
| 228 |
public String toExportString() { |
| 229 |
if (this == ANY_MAP_TYPE) { |
| 230 |
return "map(*)"; |
| 231 |
} else if (this == EMPTY_MAP_TYPE) { |
| 232 |
return "map(\u00B0)"; |
| 233 |
} else { |
| 234 |
FastStringBuffer sb = new FastStringBuffer(100); |
| 235 |
sb.append("map("); |
| 236 |
sb.append(keyType.toExportString()); |
| 237 |
sb.append(", "); |
| 238 |
sb.append(valueType.toExportString()); |
| 239 |
sb.append(")"); |
| 240 |
return sb.toString(); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public boolean equals(Object other) { |
| 249 |
if (this == other) { |
| 250 |
return true; |
| 251 |
} |
| 252 |
if (other instanceof MapType) { |
| 253 |
MapType f2 = (MapType) other; |
| 254 |
return keyType.equals(f2.keyType) && valueType.equals(f2.valueType) && mustBeEmpty == f2.mustBeEmpty; |
| 255 |
} |
| 256 |
return false; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
@Override |
| 263 |
public int hashCode() { |
| 264 |
return keyType.hashCode() ^ valueType.hashCode(); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
public int relationship(FunctionItemType other, TypeHierarchy th) { |
| 274 |
if (other == AnyFunctionType.getInstance()) { |
| 275 |
return TypeHierarchy.SUBSUMED_BY; |
| 276 |
} else if (equals(other)) { |
| 277 |
return TypeHierarchy.SAME_TYPE; |
| 278 |
} else if (other == MapType.ANY_MAP_TYPE) { |
| 279 |
return TypeHierarchy.SUBSUMED_BY; |
| 280 |
} else if (other.isArrayType()) { |
| 281 |
return TypeHierarchy.DISJOINT; |
| 282 |
} else if (other instanceof TupleItemType) { |
| 283 |
return TypeHierarchy.inverseRelationship(other.relationship(this, th)); |
| 284 |
} else if (other instanceof MapType) { |
| 285 |
|
| 286 |
|
| 287 |
MapType f2 = (MapType) other; |
| 288 |
int keyRel = th.relationship(keyType, f2.keyType); |
| 289 |
if (keyRel == TypeHierarchy.DISJOINT) { |
| 290 |
return TypeHierarchy.OVERLAPS; |
| 291 |
} |
| 292 |
int valueRel = th.sequenceTypeRelationship(valueType, f2.valueType); |
| 293 |
|
| 294 |
if (valueRel == TypeHierarchy.DISJOINT) { |
| 295 |
return TypeHierarchy.OVERLAPS; |
| 296 |
} |
| 297 |
if (keyRel == valueRel) { |
| 298 |
return keyRel; |
| 299 |
} |
| 300 |
if ((keyRel == TypeHierarchy.SAME_TYPE || keyRel == TypeHierarchy.SUBSUMES) && |
| 301 |
(valueRel == TypeHierarchy.SAME_TYPE || valueRel == TypeHierarchy.SUBSUMES)) { |
| 302 |
return TypeHierarchy.SUBSUMES; |
| 303 |
} |
| 304 |
if ((keyRel == TypeHierarchy.SAME_TYPE || keyRel == TypeHierarchy.SUBSUMED_BY) && |
| 305 |
(valueRel == TypeHierarchy.SAME_TYPE || valueRel == TypeHierarchy.SUBSUMED_BY)) { |
| 306 |
return TypeHierarchy.SUBSUMED_BY; |
| 307 |
} |
| 308 |
return TypeHierarchy.OVERLAPS; |
| 309 |
} else { |
| 310 |
int rel = TypeHierarchy.DISJOINT; |
| 311 |
|
| 312 |
rel = new SpecificFunctionType(getArgumentTypes(), getResultType()).relationship(other, th); |
| 313 |
|
| 314 |
return rel; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
@Override |
| 328 |
public Optional<String> explainMismatch(Item item, TypeHierarchy th) { |
| 329 |
if (item instanceof MapItem) { |
| 330 |
for (KeyValuePair kvp : ((MapItem)item).keyValuePairs()) { |
| 331 |
if (!keyType.matches(kvp.key, th)) { |
| 332 |
String s = "The map contains a key (" + kvp.key + ") of type " + kvp.key.getItemType() + |
| 333 |
" that is not an instance of the required type " + keyType; |
| 334 |
return Optional.of(s); |
| 335 |
} |
| 336 |
try { |
| 337 |
if (!valueType.matches(kvp.value, th)) { |
| 338 |
String s = "The map contains an entry with key (" + kvp.key + |
| 339 |
") whose corresponding value (" + Err.depictSequence(kvp.value) + |
| 340 |
") is not an instance of the required type " + valueType; |
| 341 |
Optional<String> more = valueType.explainMismatch(kvp.value, th); |
| 342 |
if (more.isPresent()) { |
| 343 |
s = s + ". " + more.get(); |
| 344 |
} |
| 345 |
return Optional.of(s); |
| 346 |
} |
| 347 |
} catch (XPathException e) { |
| 348 |
|
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
return Optional.empty(); |
| 353 |
} |
| 354 |
|
| 355 |
@Override |
| 356 |
public Expression makeFunctionSequenceCoercer(Expression exp, RoleDiagnostic role) |
| 357 |
throws XPathException { |
| 358 |
Expression result = exp; |
| 359 |
|
| 360 |
result = new SpecificFunctionType(getArgumentTypes(), getResultType()).makeFunctionSequenceCoercer(exp, role); |
| 361 |
|
| 362 |
return result; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
@Override |
| 377 |
public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) throws XPathException { |
| 378 |
if (this == ANY_MAP_TYPE) { |
| 379 |
return "return SaxonJS.U.isMap(item)"; |
| 380 |
} |
| 381 |
FastStringBuffer fsb = new FastStringBuffer(256); |
| 382 |
fsb.append("function k(item) {" + keyType.generateJavaScriptItemTypeTest(BuiltInAtomicType.ANY_ATOMIC, targetVersion) + "};"); |
| 383 |
fsb.append("function v(item) {" + valueType.getPrimaryType().generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion) + "};"); |
| 384 |
int card = valueType.getCardinality(); |
| 385 |
fsb.append(Cardinality.generateJavaScriptChecker(card)); |
| 386 |
if (targetVersion == 1) { |
| 387 |
fsb.append("return SaxonJS.U.isMap(item) && item.conforms(k, v, c);"); |
| 388 |
} else { |
| 389 |
fsb.append("return SaxonJS.U.isConstrainedMap(item, k, v, c);"); |
| 390 |
} |
| 391 |
return fsb.toString(); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
|