Debian Codegraph @

net/sf/saxon/ma/map/MapFunctionSet.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.ma.map;
9
10 import net.sf.saxon.expr.*;
11 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
12 import net.sf.saxon.expr.parser.ExpressionVisitor;
13 import net.sf.saxon.functions.InsertBefore;
14 import net.sf.saxon.functions.OptionsParameter;
15 import net.sf.saxon.functions.SystemFunction;
16 import net.sf.saxon.functions.registry.BuiltInFunctionSet;
17 import net.sf.saxon.lib.NamespaceConstant;
18 import net.sf.saxon.ma.arrays.ArrayItem;
19 import net.sf.saxon.ma.arrays.ArrayItemType;
20 import net.sf.saxon.ma.arrays.SimpleArrayItem;
21 import net.sf.saxon.om.*;
22 import net.sf.saxon.trace.ExpressionPresenter;
23 import net.sf.saxon.trans.Err;
24 import net.sf.saxon.trans.XPathException;
25 import net.sf.saxon.type.*;
26 import net.sf.saxon.value.*;
27
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Map;
31
32 /**
33 * Function signatures (and pointers to implementations) of the functions defined in XPath 2.0
34 */
35
36 public class MapFunctionSet extends BuiltInFunctionSet {
37
38 public static MapFunctionSet THE_INSTANCE = new MapFunctionSet();
39
40 public MapFunctionSet() {
41 init();
42 }
43
44 public static MapFunctionSet getInstance() {
45 return THE_INSTANCE;
46 }
47
48 private void init() {
49
50 register("merge", 1, MapMerge.class, MapType.ANY_MAP_TYPE, ONE, 0, 0)
51 .arg(0, MapType.ANY_MAP_TYPE, STAR | INS, null);
52
53 OptionsParameter mergeOptionDetails = new OptionsParameter();
54 mergeOptionDetails.addAllowedOption("duplicates", SequenceType.SINGLE_STRING, new StringValue("use-first"));
55 // duplicates=unspecified is retained because that's what the XSLT 3.0 Rec incorrectly uses
56 mergeOptionDetails.setAllowedValues("duplicates", "FOJS0005", "use-first", "use-last", "combine", "reject", "unspecified", "use-any");
57 mergeOptionDetails.addAllowedOption("duplicates-error-code", SequenceType.SINGLE_STRING, new StringValue("FOJS0003"));
58 mergeOptionDetails.addAllowedOption("Q{" + NamespaceConstant.SAXON + "}key-type", SequenceType.SINGLE_STRING, new StringValue("anyAtomicType"));
59 mergeOptionDetails.addAllowedOption("Q{" + NamespaceConstant.SAXON + "}final", SequenceType.SINGLE_BOOLEAN, BooleanValue.FALSE);
60
61 register("merge", 2, MapMerge.class, MapType.ANY_MAP_TYPE, ONE, 0, 0)
62 .arg(0, MapType.ANY_MAP_TYPE, STAR, null)
63 .arg(1, MapType.ANY_MAP_TYPE, ONE, null)
64 .optionDetails(mergeOptionDetails);
65
66 register("entry", 2, MapEntry.class, MapType.ANY_MAP_TYPE, ONE, 0, 0)
67 .arg(0, BuiltInAtomicType.ANY_ATOMIC, ONE | ABS, null)
68 .arg(1, AnyItemType.getInstance(), STAR | NAV, null);
69
70 register("find", 2, MapFind.class, ArrayItemType.getInstance(), ONE, 0, 0)
71 .arg(0, AnyItemType.getInstance(), STAR | INS, null)
72 .arg(1, BuiltInAtomicType.ANY_ATOMIC, ONE | ABS, null);
73
74 register("get", 2, MapGet.class, AnyItemType.getInstance(), STAR, 0, 0)
75 .arg(0, MapType.ANY_MAP_TYPE, ONE | INS, null)
76 .arg(1, BuiltInAtomicType.ANY_ATOMIC, ONE | ABS, null);
77
78 register("put", 3, MapPut.class, MapType.ANY_MAP_TYPE, ONE, 0, 0)
79 .arg(0, MapType.ANY_MAP_TYPE, ONE | INS, null)
80 .arg(1, BuiltInAtomicType.ANY_ATOMIC, ONE | ABS, null)
81 .arg(2, AnyItemType.getInstance(), STAR | NAV, null);
82
83 register("contains", 2, MapContains.class, BuiltInAtomicType.BOOLEAN, ONE, 0, 0)
84 .arg(0, MapType.ANY_MAP_TYPE, ONE | INS, null)
85 .arg(1, BuiltInAtomicType.ANY_ATOMIC, ONE | ABS, null);
86
87 register("remove", 2, MapRemove.class, MapType.ANY_MAP_TYPE, ONE, 0, 0)
88 .arg(0, MapType.ANY_MAP_TYPE, ONE | INS, null)
89 .arg(1, BuiltInAtomicType.ANY_ATOMIC, STAR | ABS, null);
90
91 register("keys", 1, MapKeys.class, BuiltInAtomicType.ANY_ATOMIC, STAR, 0, 0)
92 .arg(0, MapType.ANY_MAP_TYPE, ONE | INS, null);
93
94 register("size", 1, MapSize.class, BuiltInAtomicType.INTEGER, ONE, 0, 0)
95 .arg(0, MapType.ANY_MAP_TYPE, ONE | INS, null);
96
97 ItemType actionType = new SpecificFunctionType(
98 new SequenceType[]{SequenceType.SINGLE_ATOMIC, SequenceType.ANY_SEQUENCE},
99 SequenceType.ANY_SEQUENCE);
100
101 register("for-each", 2, MapForEach.class, AnyItemType.getInstance(), STAR, 0, 0)
102 .arg(0, MapType.ANY_MAP_TYPE, ONE | INS, null)
103 .arg(1, actionType, ONE | INS, null);
104
105 register("untyped-contains", 2, MapUntypedContains.class, BuiltInAtomicType.BOOLEAN, ONE, 0, 0)
106 .arg(0, MapType.ANY_MAP_TYPE, ONE | INS, null)
107 .arg(1, BuiltInAtomicType.ANY_ATOMIC, ONE | ABS, null);
108
109
110 }
111
112 @Override
113 public String getNamespace() {
114 return NamespaceConstant.MAP_FUNCTIONS;
115 }
116
117 @Override
118 public String getConventionalPrefix() {
119 return "map";
120 }
121
122
123 /**
124 * Implementation of the XPath 3.1 function map:contains(Map, key) => boolean
125 */
126 public static class MapContains extends SystemFunction {
127
128 public BooleanValue call(XPathContext context, Sequence[] arguments) throws XPathException {
129 MapItem map = (MapItem) arguments[0].head();
130 AtomicValue key = (AtomicValue) arguments[1].head();
131 return BooleanValue.get(map.get(key) != null);
132 }
133
134 }
135
136 /**
137 * Implementation of the XPath 3.1 function map:get(Map, key) => value
138 */
139 public static class MapGet extends SystemFunction {
140
141 String pendingWarning = null;
142
143 /**
144 * Method called during static type checking. This method may be implemented in subclasses so that functions
145 * can take advantage of knowledge of the types of the arguments that will be supplied.
146 *
147 * @param visitor an expression visitor, providing access to the static context and configuration
148 * @param contextItemType information about whether the context item is set, and what its type is
149 * @param arguments the expressions appearing as arguments in the function call
150 */
151 @Override
152 public void supplyTypeInformation(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType, Expression[] arguments) throws XPathException {
153 ItemType it = arguments[0].getItemType();
154 if (it instanceof TupleType) {
155 if (arguments[1] instanceof Literal) {
156 String key = ((Literal)arguments[1]).getValue().getStringValue();
157 if (((TupleType)it).getFieldType(key) == null) {
158 XPathException xe = new XPathException("Field " + key + " is not defined for tuple type " + it, "SXTT0001");
159 xe.setIsTypeError(true);
160 throw xe;
161 }
162 }
163 TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy();
164 int relation = th.relationship(arguments[1].getItemType(), BuiltInAtomicType.STRING);
165 if (relation == TypeHierarchy.DISJOINT) {
166 XPathException xe = new XPathException("Key for tuple type must be a string (actual type is " + arguments[1].getItemType(), "XPTY0004");
167 xe.setIsTypeError(true);
168 throw xe;
169 }
170 }
171 }
172
173 /**
174 * Get the return type, given knowledge of the actual arguments
175 *
176 * @param args the actual arguments supplied
177 * @return the best available item type that the function will return
178 */
179 @Override
180 public ItemType getResultItemType(Expression[] args) {
181 ItemType mapType = args[0].getItemType();
182 if (mapType instanceof TupleItemType && args[1] instanceof StringLiteral) {
183 String key = ((StringLiteral) args[1]).getStringValue();
184 TupleItemType tit = (TupleItemType) mapType;
185 SequenceType valueType = tit.getFieldType(key);
186 if (valueType == null) {
187 warning("Field " + key + " is not defined in tuple type");
188 return AnyItemType.getInstance();
189 } else {
190 return valueType.getPrimaryType();
191 }
192 } else if (mapType instanceof MapType) {
193 return ((MapType)mapType).getValueType().getPrimaryType();
194 } else {
195 return super.getResultItemType(args);
196 }
197 }
198
199 /**
200 * Get the cardinality, given knowledge of the actual arguments
201 *
202 * @param args the actual arguments supplied
203 * @return the most precise available cardinality that the function will return
204 */
205 @Override
206 public int getCardinality(Expression[] args) {
207 ItemType mapType = args[0].getItemType();
208 if (mapType instanceof TupleItemType && args[1] instanceof StringLiteral) {
209 String key = ((StringLiteral) args[1]).getStringValue();
210 TupleItemType tit = (TupleItemType) mapType;
211 SequenceType valueType = tit.getFieldType(key);
212 if (valueType == null) {
213 warning("Field " + key + " is not defined in tuple type");
214 return StaticProperty.ALLOWS_MANY;
215 } else {
216 return valueType.getCardinality();
217 }
218 } else if (mapType instanceof MapType) {
219 return Cardinality.union(
220 ((MapType) mapType).getValueType().getCardinality(),
221 StaticProperty.ALLOWS_ZERO);
222 } else {
223 return super.getCardinality(args);
224 }
225 }
226
227 /**
228 * Allow the function to create an optimized call based on the values of the actual arguments
229 *
230 * @param visitor the expression visitor
231 * @param contextInfo information about the context item
232 * @param arguments the supplied arguments to the function call. Note: modifying the contents
233 * of this array should not be attempted, it is likely to have no effect.
234 * @return either a function call on this function, or an expression that delivers
235 * the same result, or null indicating that no optimization has taken place
236 * @throws XPathException if an error is detected
237 */
238 @Override
239 public Expression makeOptimizedFunctionCall(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, Expression... arguments) throws XPathException {
240 if (pendingWarning != null && !pendingWarning.equals("DONE")) {
241 visitor.issueWarning(pendingWarning, arguments[0].getLocation());
242 pendingWarning = "DONE";
243 }
244 return null;
245 }
246
247 private void warning(String message) {
248 if (!"DONE".equals(pendingWarning)) {
249 pendingWarning = message;
250 }
251 }
252
253 public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException {
254 MapItem map = (MapItem) arguments[0].head();
255 assert map != null;
256 AtomicValue key = (AtomicValue) arguments[1].head();
257 Sequence<?> value = map.get(key);
258 if (value == null) {
259 return EmptySequence.getInstance();
260 } else {
261 return value;
262 }
263 }
264
265 }
266
267 /**
268 * Implementation of the XPath 3.1 function map:find(item()*, key) =&gt; array
269 */
270 public static class MapFind extends SystemFunction {
271
272 public ArrayItem call(XPathContext context, Sequence[] arguments) throws XPathException {
273 List<GroundedValue<?>> result = new ArrayList<>();
274 AtomicValue key = (AtomicValue) arguments[1].head();
275 processSequence(arguments[0], key, result);
276 return new SimpleArrayItem(result);
277 }
278
279 private void processSequence(Sequence<?> in, AtomicValue key, List<GroundedValue<?>> result) throws XPathException {
280 in.iterate().forEachOrFail(item -> {
281 if (item instanceof ArrayItem) {
282 for (Sequence<?> sequence : ((ArrayItem) item).members()) {
283 processSequence(sequence, key, result);
284 }
285 } else if (item instanceof MapItem) {
286 GroundedValue<?> value = ((MapItem) item).get(key);
287 if (value != null) {
288 result.add(value);
289 }
290 for (KeyValuePair entry : ((MapItem) item).keyValuePairs()) {
291 processSequence(entry.value, key, result);
292 }
293 }
294 });
295 }
296
297 }
298
299 /**
300 * Implementation of the extension function map:entry(key, value) =&gt; Map
301 */
302 public static class MapEntry extends SystemFunction {
303
304 public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException {
305 AtomicValue key = (AtomicValue) arguments[0].head();
306 assert key != null;
307 GroundedValue<?> value = ((Sequence<?>)arguments[1]).iterate().materialize();
308 return new SingleEntryMap(key, value);
309 }
310
311 /**
312 * Get the return type, given knowledge of the actual arguments
313 *
314 * @param args the actual arguments supplied
315 * @return the best available item type that the function will return
316 */
317 @Override
318 public ItemType getResultItemType(Expression[] args) {
319 PlainType ku = args[0].getItemType().getAtomizedItemType();
320 AtomicType ka;
321 if (ku instanceof AtomicType) {
322 ka = (AtomicType)ku;
323 } else {
324 ka = ku.getPrimitiveItemType();
325 }
326 return new MapType(ka,
327 SequenceType.makeSequenceType(args[1].getItemType(), args[1].getCardinality()));
328 }
329
330 public String getStreamerName() {
331 return "MapEntry";
332 }
333
334 }
335
336 /**
337 * Implementation of the extension function map:for-each(Map, Function) =&gt; item()*
338 */
339 public static class MapForEach extends SystemFunction {
340
341 public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException {
342 MapItem map = (MapItem) arguments[0].head();
343 Function fn = (Function) arguments[1].head();
344 List<GroundedValue<?>> results = new ArrayList<>();
345 for (KeyValuePair pair : map.keyValuePairs()) {
346 Sequence<?> seq = dynamicCall(fn, context, new Sequence[]{pair.key, pair.value});
347 results.add(seq.materialize());
348 }
349 return new Chain(results);
350 }
351 }
352
353 /**
354 * Implementation of the extension function map:keys(Map) =&gt; atomicValue*
355 */
356 public static class MapKeys extends SystemFunction {
357
358 public Sequence<? extends Item<AtomicValue>> call(XPathContext context, Sequence[] arguments) throws XPathException {
359 MapItem map = (MapItem) arguments[0].head();
360 assert map != null;
361 return SequenceTool.toLazySequence(map.keys());
362 }
363 }
364
365 /**
366 * Implementation of the extension function map:merge() =&gt; Map
367 * From 9.8, map:merge is also used to implement map constructors in XPath and the xsl:map
368 * instruction in XSLT. For this purpose it accepts an additional option to define the error
369 * code to be used to signal duplicates.
370 */
371 public static class MapMerge extends SystemFunction {
372
373 private String duplicates = "use-first";
374 private String duplicatesErrorCode = "FOJS0003";
375 private boolean allStringKeys = false;
376 private boolean treatAsFinal = false;
377
378 /**
379 * Allow the function to create an optimized call based on the values of the actual arguments
380 *
381 * @param visitor the expression visitor
382 * @param contextInfo information about the context item
383 * @param arguments the supplied arguments to the function call. Note: modifying the contents
384 * of this array should not be attempted, it is likely to have no effect.
385 * @return either a function call on this function, or an expression that delivers
386 * the same result, or null indicating that no optimization has taken place
387 * @throws XPathException if an error is detected
388 */
389 @Override
390 public Expression makeOptimizedFunctionCall(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, Expression... arguments) throws XPathException {
391 if (arguments.length == 2 && arguments[1] instanceof Literal) {
392 MapItem options = (MapItem) ((Literal)arguments[1]).getValue().head();
393 Map<String, Sequence<?>> values = getDetails().optionDetails.processSuppliedOptions(
394 options, visitor.getStaticContext().makeEarlyEvaluationContext());
395 String duplicates = ((StringValue) values.get("duplicates")).getStringValue();
396 String duplicatesErrorCode = ((StringValue) values.get("duplicates-error-code")).getStringValue();
397 boolean isFinal = ((BooleanValue) values.get("Q{" + NamespaceConstant.SAXON + "}final")).getBooleanValue();
398 String keyType = ((StringValue) values.get("Q{" + NamespaceConstant.SAXON + "}key-type")).getStringValue();
399 MapMerge mm2 = (MapMerge)MapFunctionSet.getInstance().makeFunction("merge", 1);
400 mm2.duplicates = duplicates;
401 mm2.duplicatesErrorCode = duplicatesErrorCode;
402 mm2.allStringKeys = keyType.equals("string");
403 mm2.treatAsFinal = isFinal;
404 return mm2.makeFunctionCall(arguments[0]);
405 }
406 return super.makeOptimizedFunctionCall(visitor, contextInfo, arguments);
407 }
408
409 /**
410 * Get the return type, given knowledge of the actual arguments
411 *
412 * @param args the actual arguments supplied
413 * @return the best available item type that the function will return
414 */
415 @Override
416 public ItemType getResultItemType(Expression[] args) {
417 ItemType it = args[0].getItemType();
418 if (it == ErrorType.getInstance()) {
419 return MapType.EMPTY_MAP_TYPE;
420 } else if (it instanceof MapType) {
421 boolean maybeCombined = true; // see bug 3980
422 if (args.length == 1) {
423 maybeCombined = false;
424 } else if (args[1] instanceof Literal) {
425 MapItem options = (MapItem) ((Literal) args[1]).getValue().head();
426 GroundedValue dupes = options.get(new StringValue("duplicates"));
427 try {
428 if (dupes == null || !"combine".equals(dupes.getStringValue())) {
429 maybeCombined = false;
430 }
431 } catch (XPathException e) {
432 //
433 }
434 }
435 if (maybeCombined) {
436 return new MapType(((MapType) it).getKeyType(),
437 SequenceType.makeSequenceType(((MapType) it).getValueType().getPrimaryType(), StaticProperty.ALLOWS_ZERO_OR_MORE));
438 } else {
439 return it;
440 }
441 } else {
442 return super.getResultItemType(args);
443 }
444 }
445
446 public MapItem call(XPathContext context, Sequence[] arguments) throws XPathException {
447 String duplicates = this.duplicates;
448 String duplicatesErrorCode = this.duplicatesErrorCode;
449 boolean allStringKeys = this.allStringKeys;
450 boolean treatAsFinal = this.treatAsFinal;
451 if (arguments.length > 1) {
452 MapItem options = (MapItem) arguments[1].head();
453 Map<String, Sequence<?>> values = getDetails().optionDetails.processSuppliedOptions(options, context);
454 duplicates = ((StringValue) values.get("duplicates")).getStringValue();
455 duplicatesErrorCode = ((StringValue) values.get("duplicates-error-code")).getStringValue();
456 treatAsFinal = ((BooleanValue) values.get("Q{" + NamespaceConstant.SAXON + "}final")).getBooleanValue();
457 allStringKeys = "string".equals(((StringValue)values.get("Q{" + NamespaceConstant.SAXON + "}key-type")).getStringValue());
458 }
459
460 if (treatAsFinal && allStringKeys) {
461 // Optimize for a map with string-valued keys that's unlikely to be modified
462 SequenceIterator iter = arguments[0].iterate();
463 DictionaryMap baseMap = new DictionaryMap();
464 MapItem next;
465 switch (duplicates) {
466 // Code is structured (a) to avoid testing "duplicates" within the loop unnecessarily,
467 // and (b) to avoid the "get" operation to look for duplicates when it's not needed.
468 case "unspecified":
469 case "use-any":
470 case "use-last":
471 while ((next = (MapItem) iter.next()) != null) {
472 for (KeyValuePair pair : next.keyValuePairs()) {
473 if (!(pair.key instanceof StringValue)) {
474 throw new XPathException("The keys in this map must all be strings (found " + pair.key.getItemType() + ")");
475 }
476 baseMap.initialPut(pair.key.getStringValue(), pair.value);
477 }
478 }
479 default:
480 while ((next = (MapItem) iter.next()) != null) {
481 for (KeyValuePair pair : next.keyValuePairs()) {
482 if (!(pair.key instanceof StringValue)) {
483 throw new XPathException("The keys in this map must all be strings (found " + pair.key.getItemType() + ")");
484 }
485 Sequence<?> existing = baseMap.get(pair.key);
486 if (existing != null) {
487 switch (duplicates) {
488 case "use-first":
489 case "unspecified":
490 case "use-any":
491 // no action
492 break;
493 case "use-last":
494 baseMap.initialPut(pair.key.getStringValue(), pair.value);
495 break;
496 case "combine":
497 InsertBefore.InsertIterator combinedIter =
498 new InsertBefore.InsertIterator(pair.value.iterate(), existing.iterate(), 1);
499 GroundedValue<?> combinedValue = combinedIter.materialize();
500 baseMap.initialPut(pair.key.getStringValue(), combinedValue);
501 break;
502 default:
503 throw new XPathException("Duplicate key in constructed map: " +
504 Err.wrap(pair.key.getStringValueCS()), duplicatesErrorCode);
505 }
506 } else {
507 baseMap.initialPut(pair.key.getStringValue(), pair.value);
508 }
509 }
510 }
511 return baseMap;
512 }
513 } else {
514 SequenceIterator iter = arguments[0].iterate();
515 MapItem baseMap = (MapItem) iter.next();
516 if (baseMap == null) {
517 return new HashTrieMap();
518 } else {
519 if (!(baseMap instanceof HashTrieMap)) {
520 baseMap = HashTrieMap.copy(baseMap);
521 }
522 MapItem next;
523 while ((next = (MapItem) iter.next()) != null) {
524 for (KeyValuePair pair : next.keyValuePairs()) {
525 Sequence<?> existing = baseMap.get(pair.key);
526 if (existing != null) {
527 switch (duplicates) {
528 case "use-first":
529 case "unspecified":
530 case "use-any":
531 // no action
532 break;
533 case "use-last":
534 baseMap = baseMap.addEntry(pair.key, pair.value);
535 break;
536 case "combine":
537 InsertBefore.InsertIterator combinedIter =
538 new InsertBefore.InsertIterator(pair.value.iterate(), existing.iterate(), 1);
539 GroundedValue<?> combinedValue = combinedIter.materialize();
540 baseMap = baseMap.addEntry(pair.key, combinedValue);
541 break;
542 default:
543 throw new XPathException("Duplicate key in constructed map: " +
544 Err.wrap(pair.key.getStringValueCS()), duplicatesErrorCode);
545 }
546 } else {
547 baseMap = baseMap.addEntry(pair.key, pair.value);
548 }
549 }
550 }
551 return baseMap;
552 }
553 }
554
555 }
556
557 public String getStreamerName() {
558 return "NewMap";
559 }
560
561 /**
562 * Export any implicit arguments held in optimized form within the SystemFunction call
563 *
564 * @param out the export destination
565 */
566 @Override
567 public void exportAdditionalArguments(SystemFunctionCall call, ExpressionPresenter out) throws XPathException {
568 if (call.getArity() == 1) {
569 HashTrieMap options = new HashTrieMap();
570 options.initialPut(new StringValue("duplicates"), new StringValue(duplicates));
571 options.initialPut(new StringValue("duplicates-error-code"), new StringValue(duplicatesErrorCode));
572 Literal.exportValue(options, out);
573 }
574 }
575 }
576
577 /**
578 * Implementation of the extension function map:put() =&gt; Map
579 */
580
581 public static class MapPut extends SystemFunction {
582
583 public MapItem call(XPathContext context, Sequence[] arguments) throws XPathException {
584
585 MapItem baseMap = (MapItem) arguments[0].head();
586
587 if (!(baseMap instanceof HashTrieMap)) {
588 baseMap = HashTrieMap.copy(baseMap);
589 }
590
591 AtomicValue key = (AtomicValue) arguments[1].head();
592 GroundedValue<?> value = ((Sequence<?>)arguments[2]).materialize();
593 return baseMap.addEntry(key, value);
594 }
595 }
596
597
598 /**
599 * Implementation of the XPath 3.1 function map:remove(Map, key) =&gt; value
600 */
601 public static class MapRemove extends SystemFunction {
602
603 public MapItem call(XPathContext context, Sequence[] arguments) throws XPathException {
604 MapItem map = (MapItem) arguments[0].head();
605 SequenceIterator iter = arguments[1].iterate();
606 AtomicValue key;
607 while ((key = (AtomicValue) iter.next()) != null) {
608 map = map.remove(key);
609 }
610 return map;
611 }
612
613 }
614
615 /**
616 * Implementation of the extension function map:size(map) =&gt; integer
617 */
618 public static class MapSize extends SystemFunction {
619
620 public IntegerValue call(XPathContext context, Sequence[] arguments) throws XPathException {
621 MapItem map = (MapItem) arguments[0].head();
622 return new Int64Value(map.size());
623 }
624 }
625
626 }