| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.ma.arrays; |
| 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.om.GroundedValue; |
| 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.util.FastStringBuffer; |
| 20 |
import net.sf.saxon.type.*; |
| 21 |
import net.sf.saxon.value.Cardinality; |
| 22 |
import net.sf.saxon.value.SequenceType; |
| 23 |
|
| 24 |
import java.util.Optional; |
| 25 |
import java.util.function.Function; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class ArrayItemType extends AnyFunctionType { |
| 32 |
|
| 33 |
public final static ArrayItemType ANY_ARRAY_TYPE = new ArrayItemType(SequenceType.ANY_SEQUENCE); |
| 34 |
|
| 35 |
public final static SequenceType SINGLE_ARRAY = |
| 36 |
SequenceType.makeSequenceType(ArrayItemType.ANY_ARRAY_TYPE, StaticProperty.EXACTLY_ONE); |
| 37 |
|
| 38 |
private SequenceType memberType; |
| 39 |
|
| 40 |
public ArrayItemType(SequenceType memberType) { |
| 41 |
this.memberType = memberType; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
@Override |
| 50 |
public Genre getGenre() { |
| 51 |
return Genre.ARRAY; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public SequenceType getMemberType() { |
| 60 |
return memberType; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
@Override |
| 70 |
public boolean isMapType() { |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
@Override |
| 81 |
public boolean isArrayType() { |
| 82 |
return true; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
@Override |
| 93 |
public String getAlphaCode() { |
| 94 |
return "FA"; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
@Override |
| 105 |
public boolean isAtomizable(TypeHierarchy th) { |
| 106 |
return true; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
@Override |
| 117 |
public PlainType getAtomizedItemType() { |
| 118 |
return memberType.getPrimaryType().getAtomizedItemType(); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public int getArity() { |
| 128 |
return 1; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public SequenceType[] getArgumentTypes() { |
| 138 |
|
| 139 |
return new SequenceType[]{BuiltInAtomicType.INTEGER.one()}; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
@Override |
| 150 |
public boolean matches(Item item, TypeHierarchy th) throws XPathException{ |
| 151 |
if (!(item instanceof ArrayItem)) { |
| 152 |
return false; |
| 153 |
} |
| 154 |
if (this == ANY_ARRAY_TYPE) { |
| 155 |
return true; |
| 156 |
} else { |
| 157 |
for (Sequence<?> s : ((ArrayItem) item).members()){ |
| 158 |
if (!memberType.matches(s, th)){ |
| 159 |
return false; |
| 160 |
} |
| 161 |
} |
| 162 |
return true; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public SequenceType getResultType() { |
| 173 |
return memberType; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public String toString() { |
| 183 |
return makeString(SequenceType::toString); |
| 184 |
} |
| 185 |
|
| 186 |
private String makeString(Function<SequenceType, String> show) { |
| 187 |
if (this.equals(ANY_ARRAY_TYPE)) { |
| 188 |
return "array(*)"; |
| 189 |
} else { |
| 190 |
FastStringBuffer sb = new FastStringBuffer(100); |
| 191 |
sb.append("array("); |
| 192 |
sb.append(show.apply(memberType)); |
| 193 |
sb.append(")"); |
| 194 |
return sb.toString(); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
@Override |
| 208 |
public String toExportString() { |
| 209 |
return makeString(SequenceType::toExportString); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public boolean equals(Object other) { |
| 217 |
if (this == other) { |
| 218 |
return true; |
| 219 |
} |
| 220 |
if (other instanceof ArrayItemType) { |
| 221 |
ArrayItemType f2 = (ArrayItemType) other; |
| 222 |
return memberType.equals(f2.memberType); |
| 223 |
} |
| 224 |
return false; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
@Override |
| 231 |
public int hashCode() { |
| 232 |
return memberType.hashCode(); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public int relationship(FunctionItemType other, TypeHierarchy th) { |
| 242 |
if (other == AnyFunctionType.getInstance()) { |
| 243 |
return TypeHierarchy.SUBSUMED_BY; |
| 244 |
} else if (equals(other)) { |
| 245 |
return TypeHierarchy.SAME_TYPE; |
| 246 |
} else if (other == ArrayItemType.ANY_ARRAY_TYPE) { |
| 247 |
return TypeHierarchy.SUBSUMED_BY; |
| 248 |
} else if (other.isMapType()){ |
| 249 |
return TypeHierarchy.DISJOINT; |
| 250 |
} else if (other instanceof ArrayItemType) { |
| 251 |
|
| 252 |
|
| 253 |
ArrayItemType f2 = (ArrayItemType) other; |
| 254 |
int rel = th.sequenceTypeRelationship(memberType, f2.memberType); |
| 255 |
return rel==TypeHierarchy.DISJOINT ? TypeHierarchy.OVERLAPS : rel; |
| 256 |
} else { |
| 257 |
int rel = TypeHierarchy.DISJOINT; |
| 258 |
|
| 259 |
rel = new SpecificFunctionType(getArgumentTypes(), getResultType()).relationship(other, th); |
| 260 |
if (rel == TypeHierarchy.SUBSUMES || rel == TypeHierarchy.SAME_TYPE) { |
| 261 |
rel = TypeHierarchy.OVERLAPS; |
| 262 |
} |
| 263 |
|
| 264 |
return rel; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
@Override |
| 269 |
public Expression makeFunctionSequenceCoercer(Expression exp, RoleDiagnostic role) |
| 270 |
throws XPathException { |
| 271 |
Expression result = exp; |
| 272 |
|
| 273 |
result = new SpecificFunctionType(getArgumentTypes(), getResultType()).makeFunctionSequenceCoercer(exp, role); |
| 274 |
|
| 275 |
return result; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
@Override |
| 288 |
public Optional<String> explainMismatch(Item item, TypeHierarchy th) { |
| 289 |
if (item instanceof ArrayItem) { |
| 290 |
for (int i=0; i<((ArrayItem)item).arrayLength(); i++) { |
| 291 |
try { |
| 292 |
GroundedValue<?> member = ((ArrayItem) item).get(i); |
| 293 |
if (!memberType.matches(member, th)) { |
| 294 |
String s = "The " + RoleDiagnostic.ordinal(i+1) + |
| 295 |
" member of the supplied array {" + |
| 296 |
Err.depictSequence(member) + |
| 297 |
"} does not match the required member type " + |
| 298 |
memberType; |
| 299 |
Optional<String> more = memberType.explainMismatch(member, th); |
| 300 |
if (more.isPresent()) { |
| 301 |
s = s + ". " + more.get(); |
| 302 |
} |
| 303 |
return Optional.of(s); |
| 304 |
} |
| 305 |
} catch (XPathException e) { |
| 306 |
return Optional.empty(); |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
return Optional.empty(); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
@Override |
| 325 |
public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) throws XPathException { |
| 326 |
if (this == ANY_ARRAY_TYPE) { |
| 327 |
return "return SaxonJS.U.isArray(item)"; |
| 328 |
} else if (targetVersion == 1) { |
| 329 |
FastStringBuffer fsb = new FastStringBuffer(256); |
| 330 |
fsb.append("function v(item) {" + memberType.getPrimaryType().generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion) + "};"); |
| 331 |
fsb.append(Cardinality.generateJavaScriptChecker(memberType.getCardinality())); |
| 332 |
fsb.append("return SaxonJS.U.isArray(item) && " + |
| 333 |
"SaxonJS.U.ForArray(item.value).every(function(seq){return c(seq.length) && SaxonJS.U.ForArray(seq).every(v)});"); |
| 334 |
return fsb.toString(); |
| 335 |
} else { |
| 336 |
|
| 337 |
FastStringBuffer fsb = new FastStringBuffer(256); |
| 338 |
fsb.append("function v(item) {" + memberType.getPrimaryType().generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion) + "};"); |
| 339 |
fsb.append(Cardinality.generateJavaScriptChecker(memberType.getCardinality())); |
| 340 |
fsb.append("return SaxonJS.U.isConstrainedArray(item, v, c);"); |
| 341 |
return fsb.toString(); |
| 342 |
|
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
|