| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.ma.arrays; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.XPathContext; |
| 11 |
import net.sf.saxon.functions.Fold; |
| 12 |
import net.sf.saxon.functions.FoldingFunction; |
| 13 |
import net.sf.saxon.functions.SystemFunction; |
| 14 |
import net.sf.saxon.functions.registry.BuiltInFunctionSet; |
| 15 |
import net.sf.saxon.lib.NamespaceConstant; |
| 16 |
import net.sf.saxon.om.*; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.type.AnyItemType; |
| 19 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 20 |
import net.sf.saxon.type.ItemType; |
| 21 |
import net.sf.saxon.type.SpecificFunctionType; |
| 22 |
import net.sf.saxon.value.*; |
| 23 |
import net.sf.saxon.z.IntHashSet; |
| 24 |
import net.sf.saxon.z.IntSet; |
| 25 |
|
| 26 |
import java.util.ArrayList; |
| 27 |
import java.util.List; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class ArrayFunctionSet extends BuiltInFunctionSet { |
| 34 |
|
| 35 |
public static ArrayFunctionSet THE_INSTANCE = new ArrayFunctionSet(); |
| 36 |
|
| 37 |
public ArrayFunctionSet() { |
| 38 |
init(); |
| 39 |
} |
| 40 |
|
| 41 |
public static ArrayFunctionSet getInstance() { |
| 42 |
return THE_INSTANCE; |
| 43 |
} |
| 44 |
|
| 45 |
private void init() { |
| 46 |
|
| 47 |
|
| 48 |
register("append", 2, ArrayAppend.class, ArrayItemType.ANY_ARRAY_TYPE, ONE, 0, 0) |
| 49 |
.arg(0, ArrayItemType.ANY_ARRAY_TYPE, ONE | INS, null) |
| 50 |
.arg(1, AnyItemType.getInstance(), STAR | NAV, null); |
| 51 |
|
| 52 |
ItemType filterFunctionType = new SpecificFunctionType( |
| 53 |
new SequenceType[]{SequenceType.ANY_SEQUENCE}, |
| 54 |
SequenceType.SINGLE_BOOLEAN); |
| 55 |
|
| 56 |
register("filter", 2, ArrayFilter.class, ArrayItemType.ANY_ARRAY_TYPE, ONE, 0, 0) |
| 57 |
.arg(0, ArrayItemType.ANY_ARRAY_TYPE, ONE | INS, null) |
| 58 |
.arg(1, filterFunctionType, ONE | INS, null); |
| 59 |
|
| 60 |
register("flatten", 1, ArrayFlatten.class, AnyItemType.getInstance(), STAR, 0, 0) |
| 61 |
.arg(0, AnyItemType.getInstance(), STAR | ABS, null); |
| 62 |
|
| 63 |
ItemType foldFunctionType = new SpecificFunctionType( |
| 64 |
new SequenceType[]{SequenceType.ANY_SEQUENCE, SequenceType.ANY_SEQUENCE}, |
| 65 |
SequenceType.ANY_SEQUENCE); |
| 66 |
|
| 67 |
register("fold-left", 3, ArrayFoldLeft.class, AnyItemType.getInstance(), STAR, 0, 0) |
| 68 |
.arg(0, ArrayItemType.ANY_ARRAY_TYPE, ONE | INS, null) |
| 69 |
.arg(1, AnyItemType.getInstance(), STAR | NAV, null) |
| 70 |
.arg(2, foldFunctionType, ONE | INS, null); |
| 71 |
|
| 72 |
register("fold-right", 3, ArrayFoldRight.class, AnyItemType.getInstance(), STAR, 0, 0) |
| 73 |
.arg(0, ArrayItemType.ANY_ARRAY_TYPE, ONE | INS, null) |
| 74 |
.arg(1, AnyItemType.getInstance(), STAR | NAV, null) |
| 75 |
.arg(2, foldFunctionType, ONE | INS, null); |
| 76 |
|
| 77 |
ItemType forEachFunctionType = new SpecificFunctionType( |
| 78 |
new SequenceType[]{SequenceType.ANY_SEQUENCE}, |
| 79 |
SequenceType.ANY_SEQUENCE); |
| 80 |
|
| 81 |
register("for-each", 2, ArrayForEach.class, AnyItemType.getInstance(), STAR, 0, 0) |
| 82 |
.arg(0, ArrayItemType.ANY_ARRAY_TYPE, ONE | INS, null) |
| 83 |
.arg(1, forEachFunctionType, ONE | INS, null); |
| 84 |
|
| 85 |
register("for-each-pair", 3, ArrayForEachPair.class, AnyItemType.getInstance(), STAR, 0, 0) |
| 86 |
.arg(0, ArrayItemType.ANY_ARRAY_TYPE, ONE | INS, null) |
| 87 |
.arg(1, ArrayItemType.ANY_ARRAY_TYPE, ONE | INS, null) |
| 88 |
.arg(2, foldFunctionType, ONE | INS, null); |
| 89 |
|
| 90 |
register("get", 2, ArrayGet.class, AnyItemType.getInstance(), STAR, 0, 0) |
| 91 |
.arg(0, ArrayItemType.ANY_ARRAY_TYPE, ONE | INS, null) |
| 92 |
.arg(1, BuiltInAtomicType.INTEGER, ONE | ABS, null); |
| 93 |
|
| 94 |
register("head", 1, ArrayHead.class, AnyItemType.getInstance(), STAR, 0, 0) |
| 95 |
.arg(0, ArrayItemType.ANY_ARRAY_TYPE, ONE | INS, null); |
| 96 |
|
| 97 |
register("insert-before", 3, ArrayInsertBefore.class, ArrayItemType.ANY_ARRAY_TYPE, ONE, 0, 0) |
| 98 |
.arg(0, ArrayItemType.ANY_ARRAY_TYPE, ONE | INS, null) |
| 99 |
.arg(1, BuiltInAtomicType.INTEGER, STAR | ABS, null) |
| 100 |
.arg(2, AnyItemType.getInstance(), STAR | NAV, null); |
| 101 |
|
| 102 |
register("join", 1, ArrayJoin.class, ArrayItemType.ANY_ARRAY_TYPE, ONE, 0, 0) |
| 103 |
.arg(0, ArrayItemType.ANY_ARRAY_TYPE, STAR | INS, null); |
| 104 |
|
| 105 |
register("put", 3, ArrayPut.class, ArrayItemType.ANY_ARRAY_TYPE, ONE, 0, 0) |
| 106 |
.arg(0, ArrayItemType.ANY_ARRAY_TYPE, ONE | INS, null) |
| 107 |
.arg(1, BuiltInAtomicType.INTEGER, STAR | INS, null) |
| 108 |
.arg(2, AnyItemType.getInstance(), STAR | NAV, null); |
| 109 |
|
| 110 |
register("remove", 2, ArrayRemove.class, ArrayItemType.ANY_ARRAY_TYPE, ONE, 0, 0) |
| 111 |
.arg(0, ArrayItemType.ANY_ARRAY_TYPE, ONE | INS, null) |
| 112 |
.arg(1, BuiltInAtomicType.INTEGER, STAR | ABS, null); |
| 113 |
|
| 114 |
register("reverse", 1, ArrayReverse.class, ArrayItemType.ANY_ARRAY_TYPE, ONE, 0, 0) |
| 115 |
.arg(0, ArrayItemType.ANY_ARRAY_TYPE, ONE | INS, null); |
| 116 |
|
| 117 |
register("size", 1, ArraySize.class, BuiltInAtomicType.INTEGER, ONE, 0, 0) |
| 118 |
.arg(0, ArrayItemType.ANY_ARRAY_TYPE, ONE | INS, null); |
| 119 |
|
| 120 |
ItemType sortFunctionType = new SpecificFunctionType( |
| 121 |
new SequenceType[]{SequenceType.ANY_SEQUENCE}, |
| 122 |
SequenceType.ATOMIC_SEQUENCE); |
| 123 |
|
| 124 |
register("sort", 1, ArraySort.class, ArrayItemType.ANY_ARRAY_TYPE, ONE, 0, 0) |
| 125 |
.arg(0, ArrayItemType.ANY_ARRAY_TYPE, ONE | INS, null); |
| 126 |
|
| 127 |
register("sort", 2, ArraySort.class, ArrayItemType.ANY_ARRAY_TYPE, ONE, 0, 0) |
| 128 |
.arg(0, ArrayItemType.ANY_ARRAY_TYPE, ONE | INS, null) |
| 129 |
.arg(1, BuiltInAtomicType.STRING, OPT | ABS, null); |
| 130 |
|
| 131 |
register("sort", 3, ArraySort.class, ArrayItemType.ANY_ARRAY_TYPE, ONE, 0, 0) |
| 132 |
.arg(0, ArrayItemType.ANY_ARRAY_TYPE, ONE | INS, null) |
| 133 |
.arg(1, BuiltInAtomicType.STRING, OPT | ABS, null) |
| 134 |
.arg(2, sortFunctionType, ONE | INS, null); |
| 135 |
|
| 136 |
register("subarray", 2, ArraySubarray.class, ArrayItemType.ANY_ARRAY_TYPE, ONE, 0, 0) |
| 137 |
.arg(0, ArrayItemType.ANY_ARRAY_TYPE, ONE | INS, null) |
| 138 |
.arg(1, BuiltInAtomicType.INTEGER, ONE | ABS, null); |
| 139 |
|
| 140 |
register("subarray", 3, ArraySubarray.class, ArrayItemType.ANY_ARRAY_TYPE, ONE, 0, 0) |
| 141 |
.arg(0, ArrayItemType.ANY_ARRAY_TYPE, ONE | INS, null) |
| 142 |
.arg(1, BuiltInAtomicType.INTEGER, ONE | ABS, null) |
| 143 |
.arg(2, BuiltInAtomicType.INTEGER, ONE | ABS, null); |
| 144 |
|
| 145 |
register("tail", 1, ArrayTail.class, ArrayItemType.ANY_ARRAY_TYPE, ONE, 0, 0) |
| 146 |
.arg(0, ArrayItemType.ANY_ARRAY_TYPE, ONE | INS, null); |
| 147 |
|
| 148 |
register("_to-sequence", 1, ArrayToSequence.class, AnyItemType.getInstance(), STAR, 0, 0) |
| 149 |
.arg(0, ArrayItemType.ANY_ARRAY_TYPE, ONE | INS, null); |
| 150 |
|
| 151 |
register("_from-sequence", 1, ArrayFromSequence.class, ArrayItemType.ANY_ARRAY_TYPE, ONE, 0, 0) |
| 152 |
.arg(0, AnyItemType.getInstance(), STAR | INS, null); |
| 153 |
|
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
public String getNamespace() { |
| 159 |
return NamespaceConstant.ARRAY_FUNCTIONS; |
| 160 |
} |
| 161 |
|
| 162 |
@Override |
| 163 |
public String getConventionalPrefix() { |
| 164 |
return "array"; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public static int checkSubscript(IntegerValue subscript) throws XPathException { |
| 177 |
int index = subscript.asSubscript(); |
| 178 |
if (index <= 0) { |
| 179 |
throw new XPathException("Array subscript " + subscript.getStringValue() + " is out of range", "FOAY0001"); |
| 180 |
} |
| 181 |
return index; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public static class ArrayAppend extends SystemFunction { |
| 189 |
|
| 190 |
public ArrayItem call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 191 |
ArrayItem array = (ArrayItem) arguments[0].head(); |
| 192 |
assert array != null; |
| 193 |
return append(array, (Sequence<?>)arguments[1]); |
| 194 |
} |
| 195 |
|
| 196 |
public static ArrayItem append(ArrayItem array, Sequence<?> member) throws XPathException { |
| 197 |
List<GroundedValue<?>> list = new ArrayList<>(1); |
| 198 |
list.add(member.materialize()); |
| 199 |
SimpleArrayItem otherArray = new SimpleArrayItem(list); |
| 200 |
return array.concat(otherArray); |
| 201 |
} |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public static class ArrayFilter extends SystemFunction { |
| 209 |
|
| 210 |
@Override |
| 211 |
public ArrayItem call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 212 |
ArrayItem array = (ArrayItem) arguments[0].head(); |
| 213 |
assert array != null; |
| 214 |
Function fn = (Function) arguments[1].head(); |
| 215 |
List<GroundedValue<?>> list = new ArrayList<>(1); |
| 216 |
int i; |
| 217 |
for (i=0; i < array.arrayLength(); i++) { |
| 218 |
if (((BooleanValue) dynamicCall(fn, context, new Sequence[]{array.get(i)}).head()).getBooleanValue()) { |
| 219 |
list.add(array.get(i)); |
| 220 |
} |
| 221 |
} |
| 222 |
return new SimpleArrayItem(list); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public static class ArrayFlatten extends SystemFunction { |
| 230 |
|
| 231 |
private void flatten(Sequence<?> arg, List<Item<?>> out) throws XPathException { |
| 232 |
arg.iterate().forEachOrFail(item -> { |
| 233 |
if (item instanceof ArrayItem) { |
| 234 |
for (Sequence<?> member : ((ArrayItem) item).members()) { |
| 235 |
flatten(member, out); |
| 236 |
} |
| 237 |
} else { |
| 238 |
out.add(item); |
| 239 |
} |
| 240 |
}); |
| 241 |
} |
| 242 |
|
| 243 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 244 |
List<Item<?>> out = new ArrayList<>(); |
| 245 |
flatten((Sequence<?>)arguments[0], out); |
| 246 |
return SequenceExtent.makeSequenceExtent(out); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
public static class ArrayFoldLeft extends SystemFunction { |
| 254 |
|
| 255 |
@Override |
| 256 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 257 |
ArrayItem array = (ArrayItem) arguments[0].head(); |
| 258 |
assert array != null; |
| 259 |
int arraySize = array.arrayLength(); |
| 260 |
Sequence<?> zero = ((Sequence<?>)arguments[1]).head(); |
| 261 |
Function fn = (Function) arguments[2].head(); |
| 262 |
int i; |
| 263 |
for (i=0; i < arraySize; i++) { |
| 264 |
zero = dynamicCall(fn, context, new Sequence[]{zero, array.get(i)}); |
| 265 |
} |
| 266 |
return zero; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
public static class ArrayFoldRight extends SystemFunction { |
| 274 |
|
| 275 |
@Override |
| 276 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 277 |
ArrayItem array = (ArrayItem) arguments[0].head(); |
| 278 |
assert array != null; |
| 279 |
Sequence<?> zero = ((Sequence<?>)arguments[1]).head(); |
| 280 |
Function fn = (Function) arguments[2].head(); |
| 281 |
int i; |
| 282 |
for (i = array.arrayLength() - 1; i >= 0; i--) { |
| 283 |
zero = dynamicCall(fn, context, new Sequence[]{array.get(i), zero}); |
| 284 |
} |
| 285 |
return zero; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
public static class ArrayForEach extends SystemFunction { |
| 293 |
|
| 294 |
@Override |
| 295 |
public ArrayItem call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 296 |
ArrayItem array = (ArrayItem) arguments[0].head(); |
| 297 |
assert array != null; |
| 298 |
Function fn = (Function) arguments[1].head(); |
| 299 |
List<GroundedValue<?>> list = new ArrayList<>(1); |
| 300 |
int i; |
| 301 |
for (i=0; i < array.arrayLength(); i++) { |
| 302 |
list.add(dynamicCall(fn, context, new GroundedValue[]{array.get(i)}).materialize()); |
| 303 |
} |
| 304 |
return new SimpleArrayItem(list); |
| 305 |
} |
| 306 |
|
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
public static class ArrayForEachPair extends SystemFunction { |
| 313 |
|
| 314 |
@Override |
| 315 |
public ArrayItem call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 316 |
ArrayItem array1 = (ArrayItem) arguments[0].head(); |
| 317 |
assert array1 != null; |
| 318 |
ArrayItem array2 = (ArrayItem) arguments[1].head(); |
| 319 |
assert array2 != null; |
| 320 |
Function fn = (Function) arguments[2].head(); |
| 321 |
List<GroundedValue<?>> list = new ArrayList<>(1); |
| 322 |
int i; |
| 323 |
for (i=0; i < array1.arrayLength() && i < array2.arrayLength(); i++) { |
| 324 |
list.add(dynamicCall(fn, context, new Sequence[]{array1.get(i), array2.get(i)}).materialize()); |
| 325 |
} |
| 326 |
return new SimpleArrayItem(list); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public static class ArrayGet extends SystemFunction { |
| 334 |
|
| 335 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 336 |
ArrayItem array = (ArrayItem) arguments[0].head(); |
| 337 |
IntegerValue index = (IntegerValue) arguments[1].head(); |
| 338 |
return array.get(checkSubscript(index) - 1); |
| 339 |
} |
| 340 |
|
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
public static class ArrayHead extends SystemFunction { |
| 347 |
|
| 348 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 349 |
ArrayItem array = (ArrayItem) arguments[0].head(); |
| 350 |
assert array != null; |
| 351 |
if (array.arrayLength() == 0){ |
| 352 |
throw new XPathException("Argument to array:head is an empty array","FOAY0001"); |
| 353 |
} |
| 354 |
return array.get(0); |
| 355 |
} |
| 356 |
|
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
public static class ArrayInsertBefore extends SystemFunction { |
| 363 |
|
| 364 |
|
| 365 |
public ArrayItem call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 366 |
ArrayItem array = (ArrayItem) arguments[0].head(); |
| 367 |
assert array != null; |
| 368 |
int index = checkSubscript((IntegerValue) arguments[1].head()) - 1; |
| 369 |
if (index < 0 || index > array.arrayLength()){ |
| 370 |
throw new XPathException("Specified position is not in range","FOAY0001"); |
| 371 |
} |
| 372 |
Sequence<?> newMember = (Sequence<?>)arguments[2]; |
| 373 |
return array.insert(index, newMember.materialize()); |
| 374 |
} |
| 375 |
|
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
public static class ArrayJoin extends SystemFunction { |
| 382 |
|
| 383 |
@Override |
| 384 |
public ArrayItem call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 385 |
SequenceIterator iterator = arguments[0].iterate(); |
| 386 |
ArrayItem array = SimpleArrayItem.EMPTY_ARRAY; |
| 387 |
ArrayItem nextArray; |
| 388 |
while ((nextArray = (ArrayItem) iterator.next()) != null) { |
| 389 |
array = array.concat(nextArray); |
| 390 |
} |
| 391 |
return array; |
| 392 |
} |
| 393 |
|
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
public static class ArrayPut extends SystemFunction { |
| 400 |
|
| 401 |
@Override |
| 402 |
public ArrayItem call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 403 |
ArrayItem array = (ArrayItem) arguments[0].head(); |
| 404 |
int index = checkSubscript((IntegerValue) arguments[1].head()) - 1; |
| 405 |
GroundedValue<?> newVal = ((Sequence<?>)arguments[2]).materialize(); |
| 406 |
return array.put(index, newVal); |
| 407 |
} |
| 408 |
|
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
public static class ArrayRemove extends SystemFunction { |
| 415 |
|
| 416 |
|
| 417 |
public ArrayItem call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 418 |
ArrayItem array = (ArrayItem) arguments[0].head(); |
| 419 |
if (arguments[1] instanceof IntegerValue) { |
| 420 |
int index = checkSubscript((IntegerValue) arguments[1]) - 1; |
| 421 |
if (index >= array.arrayLength()) { |
| 422 |
throw new XPathException("Position " + index + " is not in range", "FOAY0001"); |
| 423 |
} |
| 424 |
return array.remove(index); |
| 425 |
} |
| 426 |
IntSet positions = new IntHashSet(); |
| 427 |
SequenceIterator<IntegerValue> arg1 = (SequenceIterator<IntegerValue>)arguments[1].iterate(); |
| 428 |
arg1.forEachOrFail(pos -> { |
| 429 |
int index = checkSubscript(pos) - 1; |
| 430 |
if (index >= array.arrayLength()) { |
| 431 |
throw new XPathException("Position " + index + " is not in range", "FOAY0001"); |
| 432 |
} |
| 433 |
positions.add(index); |
| 434 |
}); |
| 435 |
return array.removeSeveral(positions); |
| 436 |
} |
| 437 |
|
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
public static class ArrayReverse extends SystemFunction { |
| 444 |
|
| 445 |
public ArrayItem call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 446 |
ArrayItem array = (ArrayItem) arguments[0].head(); |
| 447 |
assert array != null; |
| 448 |
List<GroundedValue<?>> list = new ArrayList<>(1); |
| 449 |
int i; |
| 450 |
for (i=0; i < array.arrayLength(); i++) { |
| 451 |
list.add(array.get(array.arrayLength()-i-1)); |
| 452 |
} |
| 453 |
return new SimpleArrayItem(list); |
| 454 |
} |
| 455 |
|
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
public static class ArraySize extends SystemFunction { |
| 462 |
|
| 463 |
public IntegerValue call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 464 |
ArrayItem array = (ArrayItem) arguments[0].head(); |
| 465 |
assert array != null; |
| 466 |
return new Int64Value(array.arrayLength()); |
| 467 |
} |
| 468 |
|
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
public static class ArraySubarray extends SystemFunction { |
| 475 |
|
| 476 |
|
| 477 |
public ArrayItem call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 478 |
ArrayItem array = (ArrayItem) arguments[0].head(); |
| 479 |
assert array != null; |
| 480 |
int start = checkSubscript((IntegerValue) arguments[1].head()); |
| 481 |
int length; |
| 482 |
if (arguments.length == 3) { |
| 483 |
IntegerValue len = (IntegerValue) arguments[2].head(); |
| 484 |
int signum = len.signum(); |
| 485 |
if (signum < 0) { |
| 486 |
throw new XPathException("Specified length of subarray is less than zero", "FOAY0002"); |
| 487 |
} |
| 488 |
length = signum == 0 ? 0 : checkSubscript(len); |
| 489 |
} |
| 490 |
else { |
| 491 |
length = array.arrayLength() - start + 1; |
| 492 |
} |
| 493 |
if (start < 1) { |
| 494 |
throw new XPathException("Start position is less than one","FOAY0001"); |
| 495 |
} |
| 496 |
if (start > array.arrayLength() + 1) { |
| 497 |
throw new XPathException("Start position is out of bounds","FOAY0001"); |
| 498 |
} |
| 499 |
if (start + length > array.arrayLength() + 1) { |
| 500 |
throw new XPathException("Specified length of subarray is too great for start position given","FOAY0001"); |
| 501 |
} |
| 502 |
return array.subArray(start-1, start+length-1); |
| 503 |
} |
| 504 |
|
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
public static class ArrayTail extends SystemFunction { |
| 511 |
|
| 512 |
public ArrayItem call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 513 |
ArrayItem array = (ArrayItem) arguments[0].head(); |
| 514 |
assert array != null; |
| 515 |
if (array.arrayLength() < 1){ |
| 516 |
throw new XPathException("Argument to array:tail is an empty array","FOAY0001"); |
| 517 |
} |
| 518 |
return array.remove(0); |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
public static class ArrayToSequence extends SystemFunction { |
| 528 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 529 |
ArrayItem array = (ArrayItem) arguments[0].head(); |
| 530 |
return toSequence(array); |
| 531 |
} |
| 532 |
|
| 533 |
public static Sequence<?> toSequence(ArrayItem array) throws XPathException { |
| 534 |
List<GroundedValue<?>> results = new ArrayList<>(); |
| 535 |
for (Sequence<?> seq : array.members()) { |
| 536 |
results.add(seq.materialize()); |
| 537 |
} |
| 538 |
return new Chain(results); |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
public static class ArrayFromSequence extends FoldingFunction { |
| 548 |
public ArrayItem call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 549 |
return SimpleArrayItem.makeSimpleArrayItem(((Sequence<?>)arguments[0]).iterate()); |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
@Override |
| 560 |
public Fold getFold(XPathContext context, Sequence... additionalArguments) { |
| 561 |
return new Fold() { |
| 562 |
List<GroundedValue<?>> members = new ArrayList<>(); |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
@Override |
| 570 |
public void processItem(Item<?> item) { |
| 571 |
members.add(item); |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
@Override |
| 583 |
public boolean isFinished() { |
| 584 |
return false; |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
@Override |
| 593 |
public ArrayItem result() { |
| 594 |
return new SimpleArrayItem(members); |
| 595 |
} |
| 596 |
}; |
| 597 |
} |
| 598 |
} |
| 599 |
} |