Debian Codegraph @

net/sf/saxon/ma/map/MapCreate.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.XPathContext;
11 import net.sf.saxon.functions.SystemFunction;
12 import net.sf.saxon.om.Sequence;
13 import net.sf.saxon.om.SequenceIterator;
14 import net.sf.saxon.trans.XPathException;
15
16
17 /**
18 * This is a variant of the map:new() or map:merge() function which differs in that duplicate
19 * keys are considered an error. It is not available directly to users, but underpins the map
20 * constructor expression in XPath and the xsl:map instruction in XSLT.
21 *
22 * Moved to the Saxon namespace in 9.8 - see bug 2740 and test case function-available-1017
23 */
24 public class MapCreate extends SystemFunction {
25
26 public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException {
27
28 SequenceIterator<?> iter = arguments[0].iterate();
29 MapItem baseMap = (MapItem) iter.next();
30 if (baseMap == null) {
31 return new HashTrieMap();
32 } else {
33 if (!(baseMap instanceof HashTrieMap)) {
34 baseMap = HashTrieMap.copy(baseMap);
35 }
36 MapItem next;
37 while ((next = (MapItem) iter.next()) != null) {
38 for (KeyValuePair pair : next.keyValuePairs()) {
39 if (baseMap.get(pair.key) != null) {
40 throw new XPathException("Duplicate key value (" + pair.key + ") in map", "XQDY0137");
41 } else {
42 baseMap = baseMap.addEntry(pair.key, pair.value);
43 }
44 }
45 }
46 return baseMap;
47 }
48
49 }
50
51 public String getStreamerName() {
52 return "NewMap";
53 }
54 }
55
56 // Copyright (c) 2010-2014 Saxonica Limited.